Variate color depending on the Y-value in plot
31 次查看(过去 30 天)
显示 更早的评论
Hello! I want to change the color of a line in a plot so it depends on the y-value using a colormap. I have find much code that help when you work with surfaces, but I do not know how to do it for just a line. I have managed something (see the picture) but for this I have used surf and I would like not to do that since I can't make it work with the rest of my code. (I have other lines in the same plot that I do not want to change color.) I have also managed to add a colormap to a line, but not to let it depend on the y-value. Any suggestions?
4 个评论
Adam
2016-5-2
Actually, sorry, looking at my code again I am using a surface object as a 'Line' object, of course, does not have a 'CData'. However you can use a surface to represent a line.
Adam
2016-5-2
http://uk.mathworks.com/matlabcentral/answers/5042-how-do-i-vary-color-along-a-2d-line
and
http://blogs.mathworks.com/videos/2014/08/26/making-a-multi-color-line-in-matlab/?s_tid=srchtitle
are two different ways to achieve the same kind of end result. The first is the type of idea I used which uses a surface to represent a line using some discrete number of points, the 2nd uses many line segments end to end to produce the effect of a single line.
回答(2 个)
Robert
2016-5-2
编辑:Robert
2016-5-2
Following this post on Undocumented MATLAB you could set the ColorBinding and ColorData of your line's Edge object.
x = linspace(0,40*pi,400);
y = sin(x).*cos(1.2*x);
h = plot(x,y); % capture the line handle when you plot it
cd = colormap('parula'); % take your pick (doc colormap)
cd = interp1(linspace(min(y),max(y),length(cd)),cd,y); % map color to y values
cd = uint8(cd'*255); % need a 4xN uint8 array
cd(4,:) = 255; % last column is transparency
set(h.Edge,'ColorBinding','interpolated','ColorData',cd)
5 个评论
dan brake
2018-10-9
this is really unfortunate, to have to drawnow before setting the colorbinding, etc. it makes this not so good in a loop, where i would prefer to drawnow only after the loop is over.
Stefan Weichert
2021-7-30
I know this is a very old topic, but maybe this still helps:
I draw all lines black in one for-loop and store the handles in an array. then use drawnow() once,
Then I do a second loop to do all the color-changing. Works like a charm.
actinium
2019-8-22
Hello,
Just wondering have anyone hit any limits with the edge color assignment?
nn = 25000
x = linspace(0,40*pi,nn);
y = sin(x).*cos(1.2*x);
h = plot(x,y); % capture the line handle when you plot it
cd = colormap('parula'); % take your pick (doc colormap)
cd = interp1(linspace(min(y),max(y),length(cd)),cd,y); % map color to y values
cd = uint8(cd'*255); % need a 4xN uint8 array
cd(4,:) = 255; % last column is transparency
set(h.Edge,'ColorBinding','interpolated','ColorData',cd)
works okay, but nn = 25001 doesn't seems to work, am I missing something? Thank you!
Regards,
Alan
1 个评论
Stefan Weichert
2021-7-30
I have found the EXACT same weird behaviour. 25000 points seems to be the limit for whatever reason.
I'd like to know if this can be overcome.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Orange 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!