MATLAB Answers

How could I adjust the color of multiple lines within a graph, without using the default matlab color code?

23 views (last 30 days)
Bella
Bella on 26 Aug 2020
Edited: Adam Danz on 28 Aug 2020
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')

  2 Comments

Adam Danz
Adam Danz on 27 Aug 2020
@oioi, I deleted my first answer and then noticed you had left a comment under it seconds before I deleted it.
I was able to see some of your comment shown below.
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 with gradual colors (i.e. dark blue -> light blue). What would be the proper way to do it? I attempted to do it in a couple of ways, but MATLAB kept producing line with a symbol in between, e.g. -o-, even if I did not command it to do so. Moreover, is there a way to move the legend outside of the graph?
My new answer shows you how to do this using two different methods.
If you want to avoid plotting markers, be sure not to include a marker in your LineSyle.
plot(x, y, '-') % no marker specified
My comment above this one addresses the legend question.

Sign in to comment.

Accepted Answer

Adam Danz
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.
C sets the color which will range from min(c) to max(c) (more info).
Use colormap() to change the color scheme.
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))

  0 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!