Attempting to plot one line with multiple colors

123 views (last 30 days)
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!

Accepted Answer

Walter Roberson
Walter Roberson on 17 Nov 2016
No. Line objects, and Chart Line objects, and world Primitive Chart Line Objects, all have the restriction that any one line object can only be a single color.
  2 Comments
Jbo
Jbo on 18 Nov 2016
Edited: Walter Roberson on 18 Nov 2016
We can plot multiple lines at a time. Is there a way I could build a cell array or something like:
plotArgs{i}={X(i:i+1), Y(i:i+1),'color', colorVector(i)}
When I try to do this, it works doing plot(plotArgs{1}), but plot(plotArgs{:}) throws an error.
Walter Roberson
Walter Roberson on 18 Nov 2016
Only one 'color' option will be paid attention to for plot() . You can use multiple linespec, but linespec are restricted to the color names, 'b' (blue), 'c' (cyan), 'g' (green), 'k' (black), 'm' (magenta), 'r' (red), 'w' (white) . If your colorVector is a character vector containing those codes then you could
for i = 1 : length(X)-1
plotArgs{i}={X(i:i+1), Y(i:i+1), colorVector(i)};
end
plot(plotArgs{:})
This would be equivalent to
for i = 1 : length(X) - 1
line('XData', X(i:i+1), 'YData', Y(i:i+1), 'Color', colorVector(i));
end
as plot() loops calling line(). But the line() version can be extended,
for i = 1 : length(X) - 1
line('XData', X(i:i+1), 'YData', Y(i:i+1), 'Color', colorVector(i, :));
end
and now colorVector can be a RGB table, one color triple per row.

Sign in to comment.

More Answers (1)

Lukas Klar
Lukas Klar on 9 Aug 2023
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')

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!