how to format a graph so that data point varies if..............
7 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Jack Shannon
on 2 Feb 2021
Edited: Jack Shannon
on 2 Feb 2021
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 Comments
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!