Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
777 views
in Technique[技术] by (71.8m points)

matlab - Shading an area boundered by a curve

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

EDIT: 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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...