# Is it possible to plot a curve with changing colours under a single plot?

12 ビュー (過去 30 日間)
Merse Gaspar 2023 年 5 月 28 日
コメント済み: DGM 2023 年 5 月 29 日
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?

サインインしてコメントする。

### 採用された回答

John D'Errico 2023 年 5 月 28 日

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 件のコメント表示 7 件の古いコメント非表示 7 件の古いコメント
DGM 2023 年 5 月 29 日
... 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.

サインインしてコメントする。

### その他の回答 (1 件)

Benjamin Kraus 2023 年 5 月 29 日

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 件のコメント表示 なし非表示 なし
DGM 2023 年 5 月 29 日
For some reason I can't upvote this. Let this comment be my upvote!

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangePolygons についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by