Curve based area shading

What would be the easiest way to slightly shade (or hatch or something else to distinguish it from the rest) the area in the graph (), for example, below the curve y = x ^ 2?

x = 0:pi/10:2*pi; y = x.^2.; plot(x,y); 
+6
source share
3 answers

area(x,y) should do the trick. I'm not sure this class has a FaceAlpha property.

EDIT: Unfortunately, the area class does not have the FaceAlpha property. But you can get around this and edit the patch directly:

 x=0:pi/10:2*pi; y=x.^2; H=area(x,y); h=get(H,'children'); set(h,'FaceAlpha',0.5); %#Tada! 

EDIT2: To shade the area above the curve, you can use a second area with white fill. This is a kind of kludge, but it should work. Beginning with:

 x=0:pi/10:2*pi; y=x.^2; y2=max(y)*ones(size(y)); hold on H1=area(x,y2); H2=area(x,y); set(H2,'FaceColor',[1 1 1]); axis tight 

or based on Jason S solution, use baseval input for shadow over curve:

 x=0:pi/10:2*pi; y=x.^2; baseval=max(y); H=area(x,y,baseval); h=get(H,'children'); set(h,'FaceAlpha',0.5,'FaceColor',[0 1 0]); axis tight 
+11
source

Additional Doresoom post example:

 x=0:pi/50:2*pi; y1=x.^2; y2=10+5*sin(3*x); baseval1=20; baseval2=3; clf; hold on; H1=area(x,y1,baseval1); H2=area(x,y2,baseval2); hold off; h=get(H1,'children'); set(h,'FaceAlpha',0.5,'FaceColor',[1 0.5 0]); % set color to orange, alpha to 0.5 h=get(H2,'children'); set(h,'FaceAlpha',0.5,'FaceColor',[0.85 1 0.25]); % set color to yellow-green, alpha to 0.5 

But where do you set the color?

h - patch handle (filled area); if you type get (h), you will see all its properties. MATLAB docs on patch properties explain this to some extent.

And how, for example, could you shade an area over a curve using this principle?

area creates a patch between the base value and the curve. There doesn't seem to be an easy way to create an area between two curves.

+5
source

Without messing with children you can also:

 x = 0:pi/10:2*pi; % from your example y = x.^2.; % from your example H=area(x,y); set(H(1),'FaceColor','k'); alpha(.5); 

Worked for me, it also helped with some legend problems that I had.

. Start dead question

0
source

All Articles