3

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?

Community
  • 1
  • 1
A.Rainer
  • 719
  • 5
  • 16
  • 1
    Can you add some actual code to show what you have tried? – nirvana-msu May 25 '16 at 21:55
  • I agree that the `fill()` function is not explicit in its explanation that it needs all the vertices forming a polygon of the region to be shaded between curves or lines in a strictly clockwise or counter-clockwise fashion – Pacific Stickler May 26 '16 at 18:32

1 Answers1

-1

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.

Pacific Stickler
  • 1,078
  • 8
  • 20