scatter plot with color dependent on variable.

21 views (last 30 days)
I need to plot a scatter graph, where the color goes from red to blue gradually and depends on the value of variable s (polarization value). s is calculated in excel and goes from -1 to 1. I am not sure how to achieve the above.
I am trying the following code: attached.
Any help is appreciated.
  2 Comments
Mario Malic
Mario Malic on 5 Aug 2020
Edited: Mario Malic on 5 Aug 2020
Just a rough suggestion, see documentation on Scatter properties for your MATLAB version, option is called CData. Here's an example of how to use it.
figure(1);
hb = bar(x, y);
hb.FaceColor = 'flat'; % Not needed for scatter I think
for ii = 1 : 1 : length(Color_Vector)
hb.CData(ii,:) = Color_RGB(ii,:);
end
I am not sure how one would one go from red to blue gradually (without visiting other colors). R (1, 0, 0), B (0, 0, 1), if you start with blue as the value for -1, Value for S = 0 would be (1, 0, 1), and then for S = 1, it would be red.
If S is monotonically increasing or decreasing then you could do only linspace(-1, 1, 512), where 512 stands for possible shades between those colors (you can use less, number of s values you have), if not, you can sort them, and then reorder colors by indexes.

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 5 Aug 2020
Use the c (usually the fourth) input when you call scatter. See the "Vary Circle Color" example on the documentation page. You want to specify c as either a three column matrix of RGB triplets or a vector of indices into the colormap.
  1 Comment
David Levy
David Levy on 5 Aug 2020
Hi, thank you for your answer. I have tried the above idea. like:
c = linspace(-3,3,length(kx));
scatter(kx,d(1),sz,c,'filled')
hold on
However, this produces a scatter plot with only one color.
How would I use the value of s (polarization) to gradually change colors on the scatter plot above?
In the past I have used a colormap like ‘jet’ or ‘cool’. In this case -1 to 0 changes from red to green and 0 to 1 change from green to blue, is there a way to use c to achieve the above ? Thank you.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!