对以曲线为边界的区域进行着色 [英] Shading an area boundered by a curve

查看:29
本文介绍了对以曲线为边界的区域进行着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在曲线 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) 应该可以解决问题.不过,我不确定该类是否具有 FaceAlpha 属性.

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

不幸的是,area 类没有 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!

要对曲线上方的区域进行着色,您可以使用带有白色填充的第二个区域图.这有点混乱,但它应该有效.重新开始:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆