遮蔽曲线所包围的区域 [英] Shading an area boundered by a curve
问题描述
例如,在曲线y = x ^ 2下方的plot()中,对阴影(或阴影;或与其他阴影不同的任何东西)进行着色的最简单方法是什么?
What would be the easiest way to lightly shade (or hatch; or anything to set it different from the rest) an area in a plot(), below a curve y=x^2, for example ?
x = 0:pi/10:2*pi;
y = x.^2.;
plot(x,y);
推荐答案
area(x,y)
should do the trick. I'm not sure if that class has a FaceAlpha property though.
编辑:不幸的是,区域类没有FaceAlpha属性.但是您可以解决该问题并直接编辑补丁:
Unfortunately, the area class doesn't have a FaceAlpha property. But you can work around that 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 could use a second area plot with a white fill. It's kind of a kludge, but it should work. Starting over:
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
或基于Jason S的解决方案,使用baseval输入在曲线上方绘制阴影:
or building on Jason S's solution, use the baseval input to shade above the 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
这篇关于遮蔽曲线所包围的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!