colour and 2D point-plots

June 19 2012 acer 10056
Maple

6

Someone asked me the other week whether a color gradient could be easily applied to a high density point-plot, either vertically or horizontally graded.

Without thinking, I said, "Sure, easy." But when I got to a computer, and gave it a little thought, I realized that it's not that easy to do it efficiently. And it really ought to be, even for tens of thousands of points.

There is a help-page plot,color which briefly describes some things that can be done with coloring plots. As of Maple 16, it mentions a "color data structure" which can be created by calls to the new ColorTools package. There is an example on that page for a single color, but not for several colors concurrently. Using Colortools to get a list of colors, for many points, can be done. (And there ought to be such an example.) But for the case of many data points that uses quite a lot of memory, and is slow.

Also, there is no 2D plotting equivalent to the 3D plotting colorfunc functionality. There ought to be. And just as the 3D colorfunc should be fixed to take three arguments (x,y, & z) any new 2D colorfunc should be made to take two arguments (x & y).

So, how can we apply a color gradient on a 25000 2D-point-plot, shaded by y-value? One way is to notice that the various 2D and 3D plot data structures can now store an efficient m-by-3 (or m-by-n-by-3) C_order, float[8] Array for the purpose of representing the chosen colors. (That is not documented, but can be learned by observation and inspection of various example plot structures.) We know that such an Array is relatively memory-light, and can be produced very quickly.

What this task has become is a 2D version of this method of inserting a custom made color sequence into a 3D plot, but more efficient on account of using a float[8] Array.

To get some decent timings the attached worksheet uses the time[real] command. Timings are computed both immediately after computation (same execution block) as well as after plot rendering (next execution block).

It takes about 1 sec for the Maple 16.01 64bit Standard GUI on Windows 7 to throw up and render the plot, for both methods.

It takes 3.4 sec, and a 108 MB increase in allocated memory, to compute the plot data structure result using ColorTools and a list. But it takes only 0.45 sec, and a 20.5 MB increase in allocated memory, to compute an equivalent plot data structure using the float[8] Array. (Timings on an Intel i7-960.)

[worksheet upload is misbehaving. So inlining the code.]

restart:
N:=25000:

xy:=LinearAlgebra:-RandomMatrix(N,2,generator=0.0..1.0,
                                outputoptions=[datatype=float[8]]):

str:=time[real]():

plots:-pointplot(xy,
                    color=[seq(ColorTools:-Color([xy[i,2],0,0]),i=1..N)],
                    symbolsize=4);

time[real]()-str;

                             3.323

time[real]()-str; # in new execution group

                             4.400
kernelopts(bytesalloc);

                           107646976


restart:
N:=25000:

xy:=LinearAlgebra:-RandomMatrix(N,2,generator=0.0..1.0,
                                outputoptions=[datatype=float[8]]):

str:=time[real]():

p:=plots:-pointplot(xy,color=red,symbolsize=4):

c:=Array(1..N*3,(i)->`if`(irem(i,3)=1,xy[(i+2)/3,2],0),
         datatype=float[8],order=C_order):

subsindets(p,specfunc(anything,COLOUR),z->'COLOUR'('RGB',c));

time[real]()-str;

                             0.483

time[real]()-str; # in new execution group

                             1.357
kernelopts(bytesalloc);

                            20545536

 

Ideally, I'd prefer to be able to supply the color-specifying float[8], C_order Array in the initial plotting command.

Even though the examples above have mere random data in the plane, I'd intended this to relate more to things like scatterplots of (experimentally collected, say) data.

For (non-random) uniformly spaced data in the plane there is the densityplot. The underlying structure used for 2D density plots in modern Maple also makes use of the float[8], C_order Array. In fact such a densityplot has nearly all its information contained in that Array. I'll save some (hopefully) interesting things about that structure for another post.

acer

This has been branched into the following page(s):
Please Wait...