Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shade area between horizontal line and curve in Matlab plot [duplicate]

I'm trying to shade the area above a constant horizontal line. Above the horizontal line represents data in the top 10% (i.e., 90% of my data is below the horizontal line). I used the function (curve intersect) to find the start and end of where the horizontal line intersects my data. But, I cannot figure out how to plot the area above the horizontal line, constrained by the curve. Does anyone know how to do this in Matlab? An example of my attempt is provided in the figures attached. Thank you!

Fig. 1: Black line is my horizontal constant line. The red circles represent the 'curveintersect' start and end points. I tried to plot the data to fill in the red line, but it's capturing data below the 10% line.

Black line is my horizontal constant line. The red circles represent the 'curveintersect' start and end points. I tried to plot the data to fill in the red line, but it's capturing data below the 10% line.

Fig. 2. I also attempted to use the fill function, but again, I'm capturing data outside the blue curve.

I also attempted to use the fill function, but again, I'm capturing data outside the blue curve.

Example code related to figure 2 was adopted from here (http://blogs.mathworks.com/graphics/2015/10/13/fill-between/):

mask = y2 > y1; %find where blue curve is greater than the horizontal 90th % line 
fx = [x(mask), fliplr(x(mask))];
fy = [y1(mask), fliplr(y2(mask))];
hold on
fill_color = [.929 .694 .125];
fh = fill(fx,fy,fill_color);
hold off

I have repeated the area function for 3 subplots, each with the same code, just different variables:

area(x, max(y, min(x)), min(x), 'EdgeColor','none','FaceColor', [.7 .7 .7]); alpha(.3);

The first subplot (orange stippled line) isn't plotting, but the 2nd and 3rd subplots are.

enter image description here

like image 909
user3052817 Avatar asked Feb 08 '23 13:02

user3052817


1 Answers

Use area as follows:

x = 0:.01:4*pi;  %// x data
y = sin(x);      %// y data
level = 0.5;     %// level
plot(x, y)
hold on
area(x, max(y, level), level, 'EdgeColor', 'none', 'FaceColor', [.7 .7 .7])

enter image description here

like image 129
Luis Mendo Avatar answered Feb 11 '23 08:02

Luis Mendo