How do I vary color along a 2D line?

240 ビュー (過去 30 日間)
Kathryn McCormick
Kathryn McCormick 2011 年 4 月 8 日
編集済み: Adam Danz 2023 年 1 月 31 日
I am plotting the x,y position of a point over time. I have the data in two vectors, xpos and ypos, and I plot the path of this point using plot(xpos,ypos).
I would like to have this line change color gradually, representing the time axis ( or the index of the x, y vectors).
Is there an easy way to do this?
  1 件のコメント
Rajiv
Rajiv 2014 年 2 月 10 日
Can be show line plot with different color, if not to use the surface plot as Matt did.

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

採用された回答

Matt Fig
Matt Fig 2011 年 4 月 8 日
編集済み: Adam Danz 2023 年 1 月 31 日
This is one of the classic 'tricks' of MATLAB graphics.
x = 0:.05:2*pi;
y = sin(x);
z = zeros(size(x));
col = x; % This is the color, vary with x in this case.
surface([x;x],[y;y],[z;z],[col;col],...
'facecol','no',...
'edgecol','interp',...
'linew',2);
EDIT
Changed edgecolor to interp and linewidth to 2. This looks less ragged on my machine.
  11 件のコメント
Image Analyst
Image Analyst 2021 年 1 月 19 日
Adding the plot so we can actually see what it looks like:
x = linspace(0, 2*pi, 1920); % HDTV resolution.
y = sin(x);
z = zeros(size(x));
lineColor = x; % This is the color, it varies with x in this case.
% Plot the line with width 8 so we can see the colors well.
surface([x;x], [y;y], [z;z], [lineColor;lineColor],...
'FaceColor', 'no',...
'EdgeColor', 'interp',...
'LineWidth', 8);
grid on;
Roy Goodman
Roy Goodman 2021 年 5 月 17 日
編集済み: Roy Goodman 2021 年 5 月 17 日
I tried this for something I'm working on, and am getting a weird artifact:
Each of the 6 edges of this tetrahedron are computed using the line
surface([x1 x1]',[x2 x2]',[x3 x3]',[y y]',...
'facecolor','none','edgecolor','interp','linewidth',6);
where the arrays x1, x2, x3 parameterize a straight line in 3D space, and y is constant on three of the edges and a monotonically varying sigmoidal function on the other three edges. As you can see from this image, the color index looks very non-monotonic and even noisy on the three edges where the solution varies.
The included graphic was created using the print command, but this is how it appears on the screen as well.
I've tried a few tricks to fix it, neither of which helped
  • reducing the number of colors in the colormap
  • changing the 'edgecolor' to 'flat'

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

その他の回答 (3 件)

KSSV
KSSV 2022 年 2 月 18 日
x = 0:.05:2*pi;
y = sin(x);
z = y;
patch([x nan],[y nan],[z nan],[z nan], 'edgecolor', 'interp');
colorbar;colormap(jet);
  1 件のコメント
s yuan
s yuan 2022 年 6 月 28 日
'patch' is helpful for my task, thanks a lot.

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


Daniel Refy
Daniel Refy 2015 年 3 月 7 日
N=1 numel(data(:,1)) for a=1:N:numel(data(:,1))-N plot(data(a:a+N,1),data(a:a+N,2),'.','Color',[(a/numel(data(:,1))),0,0] ) hold on end
this will change color of the line from black to red
  1 件のコメント
Ariel Szklanny
Ariel Szklanny 2018 年 12 月 2 日
編集済み: Ariel Szklanny 2018 年 12 月 2 日
This worked great for me, thanks!

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


Paulo Silva
Paulo Silva 2011 年 4 月 8 日
Something similar to this:
plot(xpos(1:10),ypos(1:10)) %first points in blue
plot(xpos(11:20),ypos(11:20),'g') %green ones
plot(xpos(21:end),ypos(21:end),'r') %final points in red
another way, the hold all function makes the plots with different color (until the list of available colors ends and after it start from the first one again)
clf
hold all
plot(xpos(1:10),ypos(1:10))
plot(xpos(11:20),ypos(11:20))
plot(xpos(21:31),ypos(21:31))
... %more plots until the end of the indexes
You can insert the code inside a for loop
clf
hold all
N=7; %every N points change color
for a=1:N:numel(xpos)-N
plot(xpos(a:a+N),ypos(a:a+N))
end
  2 件のコメント
Kathryn McCormick
Kathryn McCormick 2011 年 4 月 8 日
Thanks for the reply. I want to vary the color continuously, not in chunks.
Paulo Silva
Paulo Silva 2011 年 4 月 8 日
You can change the order of the colors and give the impression of it being continously.
axes
MyColorOrder=get(gca,'ColorOrder'); %RGB combinations per row
%make your own color order, you can even add more row (more possible colors)
set(gca,'ColorOrder',MyColorOrder); %RGB combinations per row
hold all
N=7; %every N points change color
for a=1:N:numel(xpos)-N
plot(xpos(a:a+N),ypos(a:a+N))
end

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

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by