How to shade and calculate area above a reference line in a plot?
16 views (last 30 days)
In the attached plot, I want to shade the area ONLY ABOVE the 0-crossing line without losing the curve below the 0-line.
I also want to calculate the area above the 0-crossing line.
I searched the community for answers and found several solutions to similar questions but it was not specific to this issue.
Could someone please help me? I would very much appreciate a code snippet to help solve this issue.
Robert U on 13 Mar 2020
Hi Bhaskar Ravishankar,
You can use area and logical indexing in order to shade the curve:
resX = 0.1;
xlim = [-pi,pi];
x = xlim(1):resX:xlim(2);
y = 1 * sin( x );
fh = figure;
ah = axes(fh);
ah.XGrid = 'on';
ah.XMinorGrid = 'on';
ah.YGrid = 'on';
ah.YMinorGrid = 'on';
area(ah,x(y>=0),y(y>=0)); % in order to suppress the visual uncertainties you would have to split the area, or make sure to hit the zero crossings.
Integral of the area with non-negative values:
intArea = sum(y(y>=0)*resX);