Search code examples
matlabmatlab-figure

Shade area between a straight line and plot


I have searched the web thoroughly but still found no exact solution to this. I need to shade the intersecting area above the straight line and below the plot in MATLAB.

image

I need to shade the area above the threshold at 110 and under the plot.


Solution

  • here's a way using area :

    % generate "data"
        x=1:100;
        y1=0.5*ones(100,1);
        y2=exp(-(x(:)-50).^2/500).^.5+0.1*rand(100,1);
    
    % do the plot
    
        h=area([y1(:) , (y2(:)-y1(:)).* (y2(:)>y1(:)) ]);
        h(1).FaceColor=[1 1 1];
        h(2).FaceColor=[0 0 1 ];
        hold on;
    
        plot(x,y2,'b',x,y1,'r');
    

    enter image description here