Variate color depending on the Y-value in plot
显示 更早的评论
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 个评论
You can explicitly set the 'CData' of a line object. This is what I did when I created a class for an RGB 3d line, this allows you to explicitly set the colour at each point which you can read from a colourmap if you wish or code via some other programmatic method.
In my case I wanted a line to run from a start point to an end point in RGB space which I would then colour appropriately, but since this was just a straight line I could define just choose an arbitrary number of points to define along it (I chose 100) and assign to each the appropriate colour. So my case was a little simpler
e.g. I define a line to go from [0.1 0.3 0.4] to [0.9 0.8 0.7] and it will use those coordinates both to position the line in 3d and to determine its colour end points and use a linspace between those two end points to give me my 100 colours to assign to the points.
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 个)
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 个评论
mbvoyager
2018-8-28
Thank you very much! This works for my example.
However, there is one tiny odd thing. I need to call this command manually after my script ran.
set(h.Edge,'ColorBinding','interpolated','ColorData',cd)
Adam
2018-8-28
You may need to insert a
drawnow
instruction before it.
mbvoyager
2018-9-10
Yes, this solved the problem! Thanks!
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Color and Styling 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!