Problems with using "fill" function to highlight plot background between dates
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Prudhvi Peddagoni
am 19 Jan. 2021
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");
Antworten (0)
Siehe auch
Kategorien
Mehr zu Annotations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!