- x (vector)
- y (vector)
- c (vector, ending with NaN)

23 views (last 30 days)

Hi all, I tried to create a combo graph, i.e. a line graph as well as a bar graph. I simply want the line graph to be drawn with lines of gradual colors (i.e. dark blue -> light blue). What would be the proper way to do it? MATLAB default color code does not seem to work well visually speaking.

yyaxis left;

ylabel('SMC')

plot(x1,y1,'LineWidth', 2)

hold on

plot(x1,y2,'LineWidth', 2)

plot(x1,y3,'LineWidth', 2)

plot(x1,y4,'LineWidth', 2)

plot(x1,y5,'LineWidth', 2)

hold on

yyaxis right;

ylabel ('PPT')

bar (x2, y6)

legend ('boxoff')

title('combo graph')

Adam Danz
on 26 Aug 2020

Edited: Adam Danz
on 26 Aug 2020

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.

x = linspace(-2*pi,2*pi,300);

y = sin(x);

clf

% Color changes along x axis

sp(1) = subplot(3,1,1);

c = [x(1:end-1), NaN]; % Must end in NaN to filling a solid

patch(x,y,c, 'EdgeColor', 'interp', 'LineWidth', 3)

title('Color changes along x axis')

axis tight

box on

grid on

% Color changes along y axis

sp(2) = subplot(3,1,2);

c = [y(1:end-1), NaN]; % Must end in NaN to filling a solid

patch(x,y,c, 'EdgeColor', 'interp', 'LineWidth', 3)

title('Color changes along y axis')

axis tight

box on

grid on

% Color changes according to some rule

sp(3) = subplot(3,1,3);

c = [1:150,1:149, nan]; % Must end in NaN to filling a solid

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);

% Break line into segments

x = x(:); % Force col vectors

y = y(:); % Force col vectors

xseg = [x(1:end-1),x(2:end)];

yseg = [y(1:end-1),y(2:end)];

% Plot line

clf()

h = plot(xseg',yseg','-','LineWidth',3);

% Specify colors

segColors = jet(size(xseg,1)); % Choose a colormap

set(h, {'Color'}, mat2cell(segColors,ones(size(xseg,1),1),3))

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

Start Hunting!
## 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.