2
votes

I just started learning Matlab and I want to know is there an easy way to vary the color of the scatter plot, for example, from yellow to red. I was thinking about using the command

scatter(x,y,100,c)

where c would be defined as an RGB triplet

t=linspace(0,1,100);
c='y' .* (1-t) + 'r' .* t;

Now this does something, but it doesn't really gives changes the color gradually from yellow to red. If there is an easier way or even some custom function that does this automatically, that would be great. Thanks.

1

1 Answers

1
votes

Colors are harder than that! There are infinite possible paths to go from a color to another, as color spaces are 3D volumes. Of all possible color spaces, the worst possible one to use to deal with new colors or color creation is RGB!

The reason behind this is because there is no way of knowing what color [123, 213, 42] is, and generally it wont be a color between [123, 213, 0] and [123, 213, 255].

The best option for your case is the HSV color space. The first value, Hue, contains all the color information, while the other two are the saturation and Value or intensity of the color. Just reading the first value you know wich color you are talking about.

in HSV, red is H=0 and yellow is H=60degrees, or H=60*255/360 in uint8 or H=60/255 in double. Choose a S and V value that are good for you, e.g. S=255, V=255 to have intense colors. Then:

H=linspace(0,60/255,100);
HSV=[H;ones(size(H));ones(size(H))]; 
rgb=hsv2rgb(HSV);

This will give you a colormap of the colors. Then you need to assign them properly in scatter