Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

How to add two vertical lines to plot and color between them?

Asked by Patrick

Patrick

on 4 May 2013
Accepted Answer by the cyclist

the cyclist

Hi,

I want to highlight a period in my plot by adding two vertical lines and coloring between them. Any ideas on how to do that?

0 Comments

Patrick

Patrick

Products

No products are associated with this question.

1 Answer

Answer by the cyclist

the cyclist

on 4 May 2013
Accepted answer

Here's one way:

figure
%Plot something
plot(1:10)
% Add lines
h1 = line([2 2],[1 10]);
h2 = line([5 5],[1 10]);
% Set properties of lines
set([h1 h2],'Color','k','LineWidth',2)
% Add a patch
patch([2 5 5 2],[1 1 10 10],'r')
% The order of the "children" of the plot determines which one appears on top.
% I need to flip it here.
set(gca,'children',flipud(get(gca,'children')))

3 Comments

Patrick

Patrick

on 5 May 2013

Perfect!

Patrick

Patrick

on 5 May 2013

Just one more question: instead of using the color 'r' (red), how can I use a light gray color? In fact my graph has three different patches, so I wanted to use different shades of gray...

Cheers!

the cyclist

the cyclist

on 5 May 2013

You can encode an RGB value, by replacing 'r' with an RGB vector with values between 0 and 1.

gray = [0.4 0.4 0.4];
patch([2 5 5 2],[1 1 10 10],gray)
the cyclist

the cyclist

Contact us