Shading between vertical lines in MATLAB
Asked Answered
M

2

9

I'm sure this is a simple question, but I can't seem to figure it out. I've got this plot alt text

and I want to add vertical lines and shade the area in between to highlight areas of the data. I feel like I should be able to do this using the area function, but can't seem to figure it out. The dates and values are all doubles and are two separate vectors, if that makes a difference. Any help would be greatly appreciated.

Mirellamirelle answered 15/1, 2011 at 8:32 Comment(0)
B
13

Very basic example about shading an area in a plot. Shading an area boundered by a curve might be of interest as well.

figure;
ha = area([4 6], [10 10]);
hold on
plot(1:10, 1:10,'r')
axis([1 10 1 10])
hold off

shaded area

Bubble answered 15/1, 2011 at 9:18 Comment(1)
Thank you very much. The doc for area was way too confusing.Mirellamirelle
K
2

Instead of area, you can also use fill, which can be a bit more intuitive in terms of usage.

figure;
plot(1:10, 1:10,'r');

% Define the "shading"
% Note how each x_points(i) corresponds to y_points(i)
x_points = [5, 5, 7, 7];  
y_points = [0, 10, 10, 0];
color = [0, 0, 1];

hold on;
a = fill(x_points, y_points, color);
a.FaceAlpha = 0.1;

a matlab figure

Kail answered 14/6, 2018 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.