scatter plot with color dependent on variable.
25 次查看(过去 30 天)
显示 更早的评论
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
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.
回答(1 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Scatter Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!