Variate color depending on the Y-value in plot
34 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Adam
el 2 de Mayo de 2016
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
el 2 de Mayo de 2016
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.
Respuestas (2)
Robert
el 2 de Mayo de 2016
Editada: Robert
el 2 de Mayo de 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 comentarios
dan brake
el 9 de Oct. de 2018
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
el 30 de Jul. de 2021
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
el 22 de Ag. de 2019
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 comentario
Stefan Weichert
el 30 de Jul. de 2021
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.
Ver también
Categorías
Más información sobre Orange en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!