how to format a graph so that data point varies if..............
7 次查看(过去 30 天)
显示 更早的评论
so I have a data set of 3 vectors and I am plotting x vz y. I want to vary the color of the data point based on the value of z. Any ideas as to how to do this?
z=[0,1,2,3]
x=[5.1,8.2,2,3]
x=[2.5,4.9,8,4]
0 个评论
回答(2 个)
Jack Shannon
2021-2-2
编辑:Jack Shannon
2021-2-2
Plots have an option to set the color using either a string or an RGB triplet. You can get the default color order for a figure using the syntax get(gca,'colororder'). You can reset this color order, but if you want to manually control the color of each element of your figure it's best to get/create your color order and then explicitly set the color of each plot you make.
Not sure this is what you're looking for, but here's a simple example. You have two different values of x, so I'll just use the second one:
z=[0,1,2,3];
% x=[5.1,8.2,2,3];
x=[2.5,4.9,8,4];
figure(1);
colors = get(gca,'colororder'); % matrix where each row is an RGB triplet
hold on
plot(z,x,'k-') % plot your data
for i = 1:numel(z) % loop over values of z
plot(z(i),x(i),'o',... % create a marker at point i
'color',colors(i,:),... % set the color of the marker border
'MarkerFaceColor',colors(i,:)); % set the face color of the marker
end
This creates the following plot:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/507930/image.png)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!