Problems with using "fill" function to highlight plot background between dates

1 view (last 30 days)
Ameer Fahmi
Ameer Fahmi on 11 Jan 2021
Edited: Ameer Fahmi on 19 Jan 2021
Hello everyone
I have been using "fill" function to shade the periods of financial recessions in a plot of oil return volatility, but the function does not work as I wish, I tried to do the following
%Convert excel dates to matlab dates
t = datetime(t{:,1}, 'InputFormat', 'yyyy-MM-dd');
%Specify the dates of the areas to be highlighted:
x1 = datetime(2008,6,01);
x2 = datetime(2009,6,1);
x3 = datetime(2014,6,01);
x4 = datetime(2016,6,02);
x5 = datetime(2020,1,01);
x6 = datetime(2020,7,02);
%Generate y-values for the shaded areas
y1 = [0 1.2 1.2 0];
%Create figure with the shaded areas.
%ax = axes;
fill([x1 x1 x2 x2],y1,'b', 'FaceColor',[0.800000011920929 0.800000011920929 0.800000011920929],'EdgeColor','none');
hold on;
fill([x3 x3 x4 x4],y1,'b', 'FaceColor',[0.800000011920929 0.800000011920929 0.800000011920929],'EdgeColor','none');
hold on;
fill([x5 x5 x6 x6],y1,'b', 'FaceColor',[0.800000011920929 0.800000011920929 0.800000011920929],'EdgeColor','none');
%Plot the curve lines over the highlighted areas.
plot(t,OILSV); % t is time and OILSV is volatility of oil returns
The problem is that the y values for the shaded areas must be specifcified in advance, but I want it to be automatically set up according to may data without any prior specifications, How can I do that? Is there other functions that work better than "fill" function for this purpose specifically?
  11 Comments
Ameer Fahmi
Ameer Fahmi on 19 Jan 2021
I just tried the solution you suggested with the data I have, and it works a way better than the previous code, thanks for your help

Sign in to comment.

Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!