33 views (last 30 days)

Show older comments

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 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.

Adam Danz
on 26 Aug 2020

Edited: Adam Danz
on 24 Jan 2021

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!