how to format a graph so that data point varies if..............

1 view (last 30 days)
William Hou
William Hou on 2 Feb 2021
Edited: Jack Shannon on 2 Feb 2021
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?

Answers (2)

Adam Danz
Adam Danz on 2 Feb 2021

Jack Shannon
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:
% x=[5.1,8.2,2,3];
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
This creates the following plot:


Community Treasure Hunt

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

Start Hunting!

Translated by