Search code examples
matlabplotarea

Plot Shaded Area Between Two User-Defined Lines and Beneath Preexisting Data


I am trying to add a grey shaded area between two user-defined vertical lines. I need this gray shaded area to appear beneath my already plotted data. I have tried using the fill and area functions, but haven't been able to successfully create a bounded area to appear underneath the Matlab plot. I need the shaded area plot to extend from the x-axis at vertical lines created at 5.5 and 19 and extend up to the y-axis at 900 (left y-axis) and at 1 (right y-axis). See here: https://www.dropbox.com/s/qyzkuhw17yxn8p5/sample.png


Solution

  •     % Add shaded area to plot
    
        % Line 1
        hl1 = line(x1,y1,'Color','b'); 
    
        x = [5.5 5.5 19 19]; y = [0 900 900 0]; % define edges of shaded area
    
        % Locations for 5:30am and 19:00pm with the left y-axis ranging from 0-900 
    
        hl2=fill([x(1) x(2) x(3) x(4)], [y(1) y(2) y(3) y(4)],'Color','r','EdgeColor','none','LineStyle','none'); 
    
        hl1 = line(x1,y1,'Color','b'); % Replot line 1 over shaded area
        hold on; 
    
        % Continue adding more shaded areas using the fill function or add lines directly...