I have data for 12 time points y1=[.61 .52 .45 .75 .76 .79 .82 .6 .66 .54 .43 .21];
I would like to plot this as a line plot and draw two vertical line at time point 7 and 8 and shade the area in between these two lines. This shaded area should be transparent enough to still show the line. I would also like to have a legend to show that shaded area = critical period or have "critical period" written within the area underneath the line. From this answer, I've tried:
y1=[.61 .52 .45 .75 .76 .79 .82 .6 .66 .54 .43 .21];
N=size(y1,2);
sky_blue = [0, 0, 1] ;
x=1:N;
plot([1:N]', y1, 'linewidth', 2.8, 'linestyle', '-', 'color', sky_blue);
hold on
x1=7;
x2=8;
y2=get(gca,'ylim');
plot([x1 x1],y2)
plot([x2 x2],y2)
h1 = fill(x1,x2,'b','EdgeColor','none');
The issue is that things are okay until the last line with which I can't get the shading between the two lines. Can anyone help?
You got it almost right! Simply change your last line of code to the following:
h1 = fill([x1 x1 x2 x2], [y2 fliplr(y2)], 'b','EdgeColor','none');
fill()
function takes as input the x and y coordinates of the corners (vertices) of the region (polygon) to fill the color into in either the clockwise or the anti-clockwise fashion (polygon need not be closed (fill
can close it for you)).
Here, we have passed in the vector arrays of the x and y coordinates of the four vertices of the polygon bounded by the two lines in the clockwise order starting from bottom left vertex. Note: fliplr()
function just reverses the 1x2
column vector, y2
from left to right.