Color gradient with linspace inside scatter.

24 views (last 30 days)
Hello, I'm generating a set of aligned circles with scatter, and I'd like each of the circle to have an individual color according to a color gradient. I'd like to generate this gradient as an array of dimension N, from an initial color to a final color, with the command linspace. That's a solution I initially came up with:
color=linspace(start_color,end_color,N);
scatter(p(1,1:N),p(2,1:N),color,'filled');
p(1,:) are the x coordinates of the circles. p(2,:) are the y coordinates of the circles.
What went wrong was that whatever the start_color/end_color values are I always ended up having kind of the same blue to yellow gradient. I'd also like to have access to the whole color spectrum. How can I fix this problem? Thanks in advance!

Accepted Answer

Guillaume
Guillaume on 8 Jan 2017
Edited: Guillaume on 8 Jan 2017
a) the code in your question results in circles in different sizes but all the same colour, since the colour is the fourth argument, not the third.
b) Passing scalars for the colour is not documented so I wouldn't recommend it. In any case, if you pass scalars, the scalar represent colours along the figure current colormap. By the sound of it, your figure colormap is Parula, the default since R2014b. To display the colormap on your figure:
colorbar
You'll immediately see that the range of the colormap is adjusted to match the range of color. So your linspace does nothing useful since whatever start_color and end_color you chose, start_color will map to the first colour of the colormap and end_color map to the last colour.
To answer your question, select a different colormap:
scatter(p(1, :), p(2, :), 10, 1:size(p, 2), 'filled');
colormap('jet');

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!