Attempting to plot one line with multiple colors
Mostrar comentarios más antiguos
Ok, I have looked high and low, and for the life of me I cannot find the answer. If this has already been answered please forgive me.
What I am attempting to do is plot a line with a vector of color data. I.E. plot(x, y, colorVector).
There appears to be a way to trick Mesh or such, but that's computationally slow. I've used a loop method, that plots 2 points of x and y at a time, but that eats up a lot of CPU in overhead to plot (or, I believe it does. Either way it's a lot of computational time). You can use the set call, but because of how the plotting engine works, that results in having to either wait, or drawnow which uses a lot of CPU cycles.
Is there any way that I can do like a plot(x, y, 'color', colorData) ?
Thanks!
Respuesta aceptada
Más respuestas (1)
Lukas Klar
el 9 de Ag. de 2023
Editada: Lukas Klar
el 23 de Abr. de 2025
I struggled with this one 7 years later. Here is my solution: Specify the color in a colormap and use patch with EdgeColor='flat' to plot.
x = 0:10;
y = x.^2;
% specify colors
mycolormap = [1 0 0;...
0 1 0;...
0 0 1];
% assign colors to points, make sure to use all specified colors
c = [1 2 3 2 3 1 1 3 2 1 3];
% add a NaN to the end to avoid closing of the patch
x = [x NaN];
y = [y NaN];
c = [c NaN];
% plot as patch with EdgeColor='flat'
figure
patch(x, y, c, EdgeColor='flat', Marker='.', LineWidth=2, MarkerSize=20)
colormap(mycolormap)
I my case the best solution was to make smooth transition with EdgeColor='interp'
% use EdgeColor='interp' and a smooth colormap
figure
patch(x, y, c, EdgeColor='interp', Marker='.', LineWidth=2, MarkerSize=20)
colormap('cool')
I found no perfect solution to add a legend, I couldn't find a way to alter the line in the legend. But you can paint over it. Disadvantage: it does not correctly scale if the figure is rescaled.
% store figure handle to scale at least with the initial figure size
fh = figure;
% create an empty line because the patch legend is kind of bulky
plot(nan)
hold on
% plot the real multi color line
patch(x, y, c, EdgeColor='interp', Marker='.', LineWidth=2, MarkerSize=20)
colormap('cool')
% add the legend and store its handle
lh = legend('multicolorline', Location='northwest');
% create an invisible axis on the complete figure
annotationAxes = axes(Position=[0, 0, 1, 1], Visible='off', HitTest='off');
annotationAxes.XLim = [0 1];
annotationAxes.YLim = [0 1];
% find the location of the legend line
xpatch = [0.01, 42 / fh.Position(3)] + lh.Position(1); % why 42? Because it's the correct answer
ypatch = [0, 0] + lh.Position(2) + lh.Position(4) / 2; % vertical center of the legend
% paint a multicolor line over the single color line of the legend
patch(xpatch, ypatch, [1 2], EdgeColor='interp', Marker='.', LineWidth=2, MarkerSize=20)
@Alan Hoskins I hope that does the trick for you. If you have multiple lines in the legend you have to tinker with the ypatch variable
2 comentarios
Alan Hoskins
el 17 de Abr. de 2025
Nice solution. Now is there away to make a gradient line in the legend?
Lukas Klar
el 24 de Abr. de 2025
Thanks Alan! I updated my post above.
Categorías
Más información sobre Orange en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


