MATLAB,填充两组数据之间的区域,一行中的线条

Jos*_*osh 40 matlab plot area fill

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

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

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

我希望这是有道理的.

Dor*_*oom 54

基于@ 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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

通过翻转x数组并将其与原始数据连接起来,您可以向外,向下,向后,然后向上关闭一个完整的,多边多边形的多边形阵列.


小智 13

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

fill_between_lines = @(X,Y1,Y2,C) fill( [X fliplr(X)],  [Y1 fliplr(Y2)], C );
Run Code Online (Sandbox Code Playgroud)


gno*_*ice 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
Run Code Online (Sandbox Code Playgroud)

这是最终的数字:

在此输入图像描述

通过修改axes对象'Children'属性中的控制顺序,可以在绘制对象后更改图形中对象的堆叠顺序.例如,此代码反转堆叠顺序,将绿色多边形隐藏在蓝色多边形后面:

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

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

set(h2,'FaceAlpha',0.5);
Run Code Online (Sandbox Code Playgroud)


Ale*_*lex 5

您想要查看 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]);
Run Code Online (Sandbox Code Playgroud)

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