How can I plot two variables with the line color varying as the third variable?

My goal is to plot two variables, and vary the color of the lines by the third variable. I've done this before using scatter easily with the following code:
figure(2)
scatter(dist,geoPga,[],mag,'filled')
c = colorbar;
c.Label.String = 'Moment Magnitude, Mw'
xlabel('Hypocentral Distance to Station (km)')
ylabel('Geometric PGA (cm/s/s)')
title('Geometric PGA vs. Distance to Station')
But trying something similar with plot doesn't seem to work... Here is what I have tried so far...
figure(1)
plot(1./T,ScaledSa,[],log10stddev,'Filled')
c = colorbar;
c.Label.String = 'Standard Deviation'
title('Target Spectrum & Scaled
Records','FontWeight','bold','FontSize',14)
xlabel('Frequency (Hz)','FontWeight','bold','FontSize',12)
ylabel('PSA (cm/s/s)','FontWeight','bold','FontSize',12)
xlim([0 50])
hold on
Essentially, I'm trying to show the standard deviation as the third variable... With a certain color lines representing small standard deviations, and a gradient of different colors up to a high standard deviation.
Any tips?

1 Comment

Same need, this is so easy in e.g. R, no idea in Matlab...did you solve it?

Sign in to comment.

Answers (2)

x = linspace(0,2*pi,1000) ;
y = sin(x);
z = zeros(size(x));
c = x; % This is the color, vary with x in this case.
surface([x;x],[y;y],[z;z],[c;c],'facecolor','none','edgecolor','interp','linewidth',2);

1 Comment

This isn't what I'm looking for. This varies the color along a particular line. I have 68 total lines, and I want each line to be only one color. Each line has an associated standard deviation, and I want to plot the lines with the smallest standard deviation being green, with a scale, up to the largest standard deviation being red.

Sign in to comment.

Use patch
x = linspace(0,10);
y = sin(x);
c = jet(100);
n = length(x);
fv = [1:n-1;2:n]';
patch('faces',fv,'vertices',[x; y]',...
'faceVertexCData',c,...
'edgecolor','flat',...
'linewidth',2)

Asked:

on 11 Sep 2018

Answered:

on 30 Mar 2020

Community Treasure Hunt

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

Start Hunting!