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

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

This code seems to work only with numeric dates, I need function that work with date format
hello
so back to your code
you know the min / max values of the data to display, so why not use this info in
caxis
to scale your plot and use also same info as x and y values for the shaded area ?
I'm currently doing this, but I need to this for many other figures (14 figures), also my data have different scals and it is a bit combersome to do it manualy, so I was looking for a unified function for all the figures to make it more easy and fast, I tried "vfill" function but it didn't work
so why not looking first at the min / max values for all plots first and display only once the common axis values are defined ?
It does not help much if I did that, because some other data range over very small scall, so I would need to go back and restore the original scale manually in the figure of this data, it is pretty much like what you said previously
Hi,
What is the issue if you replace 1.2 in y1 variable with max(OILSV) ? This should solve the problem right?
I might have not explained my problem well in my first post, so I'm sorry for that
The other variables which I intend to plot it over the shaded areas of oil price declines are completely different from the variable of oil return volatility, some of them are larger and other are smaller, therefore using max(OILSV) and min(OILSV) will not help
Hi,
So my understanding of the issue is, You don't have data about Y values before hand. To solve this, You can first plot all the oil return volatility. Then you can use fill function to plot the recession. Then you need to use ax.YLim to get the y values of the axes ax. Then you have to use uistack function to push these plots created by fill function to the back.
x1 = 1;
x2 = 2;
x3 = 3;
x4 = 4;
x5 = 5;
x6 = 6;
ax = axes;
x = 1 : pi/100:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(ax,x,y1);
hold on
plot(ax,x,y2);
hold on
y1 = [ax.YLim(1) ax.YLim(2) ax.YLim(2) ax.YLim(1)];
f1 = fill(ax,[x1 x1 x2 x2],y1,'b', 'FaceColor',[0.800000011920929 0.800000011920929 0.800000011920929],'EdgeColor','none');
hold on;
f2 = fill(ax,[x3 x3 x4 x4],y1,'b', 'FaceColor',[0.800000011920929 0.800000011920929 0.800000011920929],'EdgeColor','none');
hold on;
f3 = fill(ax,[x5 x5 x6 x6],y1,'b', 'FaceColor',[0.800000011920929 0.800000011920929 0.800000011920929],'EdgeColor','none');
uistack(f1,"bottom");
uistack(f2,"bottom");
uistack(f3,"bottom");
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)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Release

R2018a

Asked:

on 11 Jan 2021

Edited:

on 19 Jan 2021

Community Treasure Hunt

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

Start Hunting!