Is it possible to plot a curve with changing colours under a single plot?
68 views (last 30 days)
Show older comments
A would like to plot a curve with different colour line segments, and I need to do it under a single plot handle. Is it possible to do with the standard plot function, or I need something else?
0 Comments
Accepted Answer
John D'Errico
on 28 May 2023
Edited: John D'Errico
on 28 May 2023
Arguably, this is something the line function would allow you to do. But line allows you to specify only a single color for an entire line. plot is also unable to do as you wish.
You could, if you wanted, plot each segment of some line using different colors. For example,
n = 100;
x = linspace(0,1,n);
y = exp(x);
C = jet(n-1);
C = mat2cell(C,ones(n-1,1),3);
H = line([x(1:n-1);x(2:n)],[y(1:n-1);y(2:n)]);
[H.Color] = deal(C{:});
Yes, this is a bit tricky, and it requires that you understand how comma separated lists work, and how to use them. Again, I'd have argued plot or line should have the capability to do it directly, certainly so for line. I did it using line, but it was a bit of a kluge in my eyes. Effectively, there are 99 distinct segments there, each of which has its own specified color.
8 Comments
DGM
on 29 May 2023
Oh. I missed the point about thick lines. You could do something like you suggest with adjacent pairs, but you'll still get weird overlapping even though there aren't gaps. You can change the mitering method, but not the end caps.
n = 10;
x = rand(1,n);
y = rand(1,n);
C = jet(n-2);
C = mat2cell(C,ones(n-2,1),3);
xx = [x(1:n-2); x(2:n-1); x(3:n)];
yy = [y(1:n-2); y(2:n-1); y(3:n)];
H = line(xx,yy,'linewidth',7);
[H.Color] = deal(C{:});
This also means that the last color is applied to two visible segments.
DGM
on 29 May 2023
... Given how bad that looks, you could always consider using a pseudoline generated via scatter(). It's still not going to give you crisp symmetric miters, but I think it looks marginally better.
nverts = 10;
x = rand(1,nverts);
y = rand(1,nverts);
% oversample everything by some factor
ptsperseg = 1000;
CT = jet(nverts-1);
CT = repelem(CT,ptsperseg,1);
pf = linspace(1,nverts,(nverts-1)*ptsperseg);
xf = interp1(x,pf,'linear');
yf = interp1(y,pf,'linear');
scatter(xf,yf,20,CT,'filled')
That also fixes the issues with the last two segments being colored the same.
More Answers (1)
Benjamin Kraus
on 29 May 2023
Edited: Benjamin Kraus
on 29 May 2023
As others have noted, you cannot use the plot or line commands to create a multi-color line in MATLAB today, but this is possible in MATLAB, without using multiple line objects or even more scatter points.
The best way to plot a single multi-color line in MATLAB today is using the patch command. In fact, there is even an example in the doc that shows how to do it.
Patches are ordinarily used to draw filled regions, but if you insert a NaN into the vertices, then patch can be used to draw lines instead. And, multi-color lines in a Patch object is fully supported and documented, by setting the EdgeColor to interp and then setting either the CData or FaceVertexCData.
Here is an amended version of the example from the doc that includes wide lines and adjusting the LineJoin to get a different connection between individual segments:
figure
x = linspace(1,10,15);
y = sin(x);
y(end) = NaN;
c = y;
patch(x,y,c,'EdgeColor','interp','LineWidth',5,'LineJoin','round');
And, to emphasize how LineJoin works in this case, here is another example:
figure
nverts = 10;
x = rand(nverts,1);
y = rand(nverts,1);
c = turbo(nverts+1);
v = [x y; NaN NaN];
f = 1:nverts+1;
patch('Vertices',v, ...
'Faces',f, ...
'FaceVertexCData',c, ...
'EdgeColor','interp', ...
'LineWidth',10, ...
'LineJoin','round');
1 Comment
See Also
Categories
Find more on Graphics Object Programming 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!