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 Comments

Adam
Adam on 29 Apr 2016
Edited: Adam on 29 Apr 2016
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.
Johanna
Johanna on 2 May 2016
Edited: Johanna on 2 May 2016
Hm, I think I tried something like that. But if I do this, will not the other lines get the same properties then? It would be great if you could give a code example :)
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.
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.

Sign in to comment.

Answers (2)

Robert
Robert on 2 May 2016
Edited: Robert on 2 May 2016
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 Comments

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)
You may need to insert a
drawnow
instruction before it.
Yes, this solved the problem! Thanks!
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.
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.

Sign in to comment.

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 Comment

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.

Sign in to comment.

Asked:

on 29 Apr 2016

Commented:

on 30 Jul 2021

Community Treasure Hunt

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

Start Hunting!