Shade an area in a plot between two Y values

Dear MATLAB community,
I have a time series plot, and would like to shade specific regions in light grey (transparent).
The time series plot is an economic process, and I would like to shade times of recessions.
That is, I already have the values of the y-axis (the respective start date and end date of a recession), and I already have a nice plot, but I cannot figure out how to add these 'recession shadings'.
In short: I would like to shade an area in a time series plot, and the area is defined as:
X(:) % all x
Y(y1,y2) % between y1 and y2
Thank you!

 채택된 답변

Star Strider
Star Strider 2015년 9월 16일
편집: Star Strider 2015년 9월 16일

5 개 추천

See if this does what you want:
x = linspace(0, 10, 10);
y = randi(9, 1, 10);
ybars = [2 6];
figure(1)
hp = plot(x, y, 'bp');
hold on
patch([min(xlim) max(xlim) max(xlim) min(xlim)], [ybars(1) ybars(1), ybars(2) ybars(2)], [0.8 0.8 0.8])
plot(x, y, 'bp')
hold off
axis([0 10 0 10])
You will have to modifiy it to do what you want, but that should be straightforward. Note the repeated plot call to prevent the patch from hiding it.

댓글 수: 7

My bad, I made a mistake!
I swapped X and Y:
Obviously, X is the t-axis, and Y is the value axis of my process.
Thus, I am looking for a way to shade two X values, for all Y.
Thank you Star Strider, sorry for the wrong question specs
Simple modification of my previous code:
x = linspace(0, 10, 10);
y = randi(9, 1, 10);
xbars = [2 6];
figure(1)
hp = plot(x, y, 'bp');
hold on
patch([xbars(1) xbars(1), xbars(2) xbars(2)], [min(xlim) max(xlim) max(xlim) min(xlim)], [0.8 0.8 0.8])
plot(x, y, 'bp')
hold off
axis([0 10 0 10])
Again, you will have to modify it to do what you want in your application, but the modifications should be relatively straightforward to do.
Thank you, star strider!
As always, I much appreciate your help!
And as always, my pleasure!
jen Magnes
jen Magnes 2018년 7월 11일
Thank you! Very helpful!
As always, my pleasure!
In addition to Start Strider's solution, starting in R2023a you can use the xregion and yregion functions:
plot([datetime(2020, 1, 1), datetime(2023, 1, 1)], [1, 2]);
xregion(datetime(2021, 1, 1), datetime(2022, 1, 1));

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Narendra Sharma MadanLal Sharma
Narendra Sharma MadanLal Sharma 2017년 8월 14일

0 개 추천

Thanks a lot ,Star Strider!

댓글 수: 2

My pleasure!
Didn't spend more than a minute on this so possibly there is more to it, but if using a legend, both the patch and the double call to plot will affect the current spot in the ordered list of plot colors. Set the patch handlevisibility to 'off' and use cla after the first plot, if you don't want to bother dealing with the color order in a more programmatic fashion.

댓글을 달려면 로그인하십시오.

A K M Kamrul Hasan
A K M Kamrul Hasan 2020년 1월 22일

0 개 추천

Hi,
This discussion is about the srea bounded by straight lines. However, is it possible to shade an area in Matlab plot bounded by straight lines and curved lines? (i.e like the attached image) ?
Regards,
Kamrul Hasan 1486289316.png

댓글 수: 3

Yes.
May I know the necessary commands for that?
Just seeing this now.
Yes! It is definitely possible.
x = linspace(0, pi);
y = sin(x);
Pm = 0.5;
xPm = interp1(y(x<=pi/2), x(x<=pi/2), Pm) % Calculate Interseection
xPm = 0.5237
deltam = interp1(y(x>=pi/2), x(x>=pi/2), Pm) % Calculate Interseection
deltam = 2.6179
figure
plot(x, y)
Lv1 = (x>=pi/2) & (y>=Pm); % Logical Vector
Lv2 = (x>=xPm) & (x<=pi/2); % Logical Vector
patch([x(Lv1) flip(x(Lv1))], [ones(size(x(Lv1)))*Pm flip(y(Lv1))], 'r' ) % Call 'patch'
patch([x(Lv2) flip(x(Lv2))], [zeros(size(x(Lv2))) ones(size(y(Lv2)))*Pm], 'r') % Call 'patch'
yline(Pm,'-k','P_m', 'LabelHorizontalAlignment','left', 'FontWeight','bold')
text(median(x(Lv2)), Pm/2, 'A_1', 'Horiz','center', 'Vert','top')
text(median(x(Lv1)), (max(y)+Pm)/2, 'A_2', 'Horiz','right', 'Vert','top')
hold on
plot([1 1]*deltam, [0 Pm], '--k')
hold off
It’s worth noting how ‘Lv’ is constructed. It combines the logical values where and to create the appropriate bounding region to fill with patch.
Add the other text calls to create text in the appropriate places. The x-axis location for has been calculated.
.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

질문:

2015년 9월 15일

댓글:

2023년 3월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by