Here are two demos covering two different methods.
Patch demo
Here's a demo using patch() to vary the color of a line.
Each line is defined by 3 variable of equal length
- x (vector)
- y (vector)
- c (vector, ending with NaN)
X and Y define the line coordinates.
C sets the color which will range from min(c) to max(c) (more info). x = linspace(-2*pi,2*pi,300);
y = sin(x);
clf
sp(1) = subplot(3,1,1);
c = [x(1:end-1), NaN];
patch(x,y,c, 'EdgeColor', 'interp', 'LineWidth', 3)
title('Color changes along x axis')
axis tight
box on
grid on
sp(2) = subplot(3,1,2);
c = [y(1:end-1), NaN];
patch(x,y,c, 'EdgeColor', 'interp', 'LineWidth', 3)
title('Color changes along y axis')
axis tight
box on
grid on
sp(3) = subplot(3,1,3);
c = [1:150,1:149, nan];
patch(x,y,c, 'EdgeColor', 'interp', 'LineWidth', 3)
title('Color changes according to some rule')
axis tight
box on
grid on
Segmentation demo
This demo breaks apart the line into segments which requires that the line is defined by at least 3 coordinates.
It then assigns colors based on your selected nx3 RGB colormap.
x = linspace(-2*pi,2*pi,300);
y = sin(x);
x = x(:);
y = y(:);
xseg = [x(1:end-1),x(2:end)];
yseg = [y(1:end-1),y(2:end)];
clf()
h = plot(xseg',yseg','-','LineWidth',3);
segColors = jet(size(xseg,1));
set(h, {'Color'}, mat2cell(segColors,ones(size(xseg,1),1),3))
2 Comments
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/585011-how-could-i-adjust-the-color-of-multiple-lines-within-a-graph-without-using-the-default-matlab-colo#comment_986990
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/585011-how-could-i-adjust-the-color-of-multiple-lines-within-a-graph-without-using-the-default-matlab-colo#comment_986990
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/585011-how-could-i-adjust-the-color-of-multiple-lines-within-a-graph-without-using-the-default-matlab-colo#comment_986996
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/585011-how-could-i-adjust-the-color-of-multiple-lines-within-a-graph-without-using-the-default-matlab-colo#comment_986996
Sign in to comment.