scatter plot with color dependent on variable.

조회 수: 8 (최근 30일)
David Levy
David Levy 2020년 8월 5일
댓글: David Levy 2020년 8월 5일
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
Mario Malic
Mario Malic 2020년 8월 5일
편집: Mario Malic 2020년 8월 5일
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.
David Levy
David Levy 2020년 8월 5일
Thank you for your comment.

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2020년 8월 5일
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
David Levy
David Levy 2020년 8월 5일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by