Problems with using "fill" function to highlight plot background between dates
Mostrar comentarios más antiguos
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 comentarios
Mathieu NOE
el 11 de En. de 2021
hello
I believe this has been more or less addressed here
Ameer Fahmi
el 13 de En. de 2021
Editada: Ameer Fahmi
el 13 de En. de 2021
Mathieu NOE
el 13 de En. de 2021
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 ?
Mathieu NOE
el 13 de En. de 2021
sorry I meant axis not caxis
Ameer Fahmi
el 13 de En. de 2021
Editada: Ameer Fahmi
el 13 de En. de 2021
Mathieu NOE
el 13 de En. de 2021
so why not looking first at the min / max values for all plots first and display only once the common axis values are defined ?
Ameer Fahmi
el 13 de En. de 2021
Prudhvi Peddagoni
el 18 de En. de 2021
Hi,
What is the issue if you replace 1.2 in y1 variable with max(OILSV) ? This should solve the problem right?
Ameer Fahmi
el 18 de En. de 2021
Editada: Ameer Fahmi
el 18 de En. de 2021
Prudhvi Peddagoni
el 19 de En. de 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");
Ameer Fahmi
el 19 de En. de 2021
Editada: Ameer Fahmi
el 19 de En. de 2021
Respuestas (0)
Categorías
Más información sobre Graphics Performance en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!