41

我有一个关于使用该area功能的问题;或者也许另一个功能是为了......我从一个大文本文件创建了这个图:

http://img818.imageshack.us/img818/9519/iwantthisareafillin.jpg

绿色和蓝色代表两个不同的文件。我想要做的是分别填写红线和每次运行之间的区域。我可以用类似的想法创建一个区域图,但是当我将它们绘制在同一个图上时,它们不会正确重叠。本质上,4 个地块将在一个图形上。

我希望这是有道理的。

4

4 回答 4

56

根据@gnovice 的回答,您实际上可以仅在两条曲线之间的区域创建带有阴影的填充图。只需fill与 结合使用fliplr

例子:

x=0:0.01:2*pi;                  %#initialize x array
y1=sin(x);                      %#create first curve
y2=sin(x)+.5;                   %#create second curve
X=[x,fliplr(x)];                %#create continuous x value array for plotting
Y=[y1,fliplr(y2)];              %#create y values for out and then back
fill(X,Y,'b');                  %#plot filled area

在此处输入图像描述

通过翻转 x 数组并将其与原始数组连接,您将向外、向下、向后、然后向上关闭两个数组,形成一个完整的多对多多面多边形。

于 2011-09-26T21:57:57.467 回答
14

就个人而言,我觉得包装填充功能既优雅又方便。要在两个大小相等Y1Y2共享支持X(和颜色 C)的行向量之间填充:

fill_between_lines = @(X,Y1,Y2,C) fill( [X fliplr(X)],  [Y1 fliplr(Y2)], C );
于 2013-05-17T19:59:05.347 回答
11

您可以使用FILL函数在绘图部分下创建填充多边形来完成此操作。您将希望按照您希望它们在屏幕上堆叠的顺序绘制线条和多边形,从最底部开始。这是一个带有一些示例数据的示例:

x = 1:100;             %# X range
y1 = rand(1,100)+1.5;  %# One set of data ranging from 1.5 to 2.5
y2 = rand(1,100)+0.5;  %# Another set of data ranging from 0.5 to 1.5
baseLine = 0.2;        %# Baseline value for filling under the curves
index = 30:70;         %# Indices of points to fill under

plot(x,y1,'b');                              %# Plot the first line
hold on;                                     %# Add to the plot
h1 = fill(x(index([1 1:end end])),...        %# Plot the first filled polygon
          [baseLine y1(index) baseLine],...
          'b','EdgeColor','none');
plot(x,y2,'g');                              %# Plot the second line
h2 = fill(x(index([1 1:end end])),...        %# Plot the second filled polygon
          [baseLine y2(index) baseLine],...
          'g','EdgeColor','none');
plot(x(index),baseLine.*ones(size(index)),'r');  %# Plot the red line

这是结果图:

在此处输入图像描述

您还可以通过修改轴对象'Children'属性中的句柄顺序来更改图中对象的堆叠顺序。例如,此代码反转堆叠顺序,将绿色多边形隐藏在蓝色多边形后面:

kids = get(gca,'Children');        %# Get the child object handles
set(gca,'Children',flipud(kids));  %# Set them to the reverse order

最后,如果您不确切知道要提前堆叠多边形的确切顺序(即,任何一个都可能是较小的多边形,您可能想要在顶部),那么您可以调整'FaceAlpha'属性,以便一个或两个多边形将显示为部分透明并在其下方显示另一个。例如,以下将使绿色多边形部分透明:

set(h2,'FaceAlpha',0.5);
于 2011-06-06T13:22:29.443 回答
4

您想查看 patch() 函数,并潜入水平线起点和终点的点:

x = 0:.1:2*pi;
y = sin(x)+rand(size(x))/2;

x2 = [0 x 2*pi];
y2 = [.1 y .1];
patch(x2, y2, [.8 .8 .1]);

如果您只想要部分数据的填充区域,则需要截断 x 和 y 向量以仅包含您需要的点。

于 2011-06-05T23:34:50.673 回答