在MATLAB中,填充两组数据之间的区域,一个图形中的线条。

时间:2023-02-09 20:22:35

I have a question about using the area function; or perhaps another function is in order... I created this plot from a large text file:

我有一个关于使用面积函数的问题;或者另一个函数是有序的…我从一个大的文本文件中创建了这个图表:

在MATLAB中,填充两组数据之间的区域,一个图形中的线条。

The green and the blue represent two different files. What I want to do is fill in the area between the red line and each run, respectively. I can create an area plot with a similar idea, but when I plot them on the same figure, they do not overlap correctly. Essentially, 4 plots would be on one figure.

绿色和蓝色代表两个不同的文件。我要做的是分别填充红色线和每个运行的区域。我可以用一个类似的方法来创建一个区域图,但是当我把它们画在同一个图上时,它们不能正确地重叠。从本质上讲,4个情节是一个数字。

I hope this makes sense.

我希望这是有意义的。

4 个解决方案

#1


49  

Building off of @gnovice's answer, you can actually create filled plots with shading only in the area between the two curves. Just use fill in conjunction with fliplr.

通过@ gnew的回答,你可以创建一个只在两个曲线之间的区域的填充图。只需要和fliplr一起使用。

Example:

例子:

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

在MATLAB中,填充两组数据之间的区域,一个图形中的线条。

By flipping the x array and concatenating it with the original, you're going out, down, back, and then up to close both arrays in a complete, many-many-many-sided polygon.

通过翻转x数组并将其与原来的数组连接起来,您将会在一个完整的、多用途的多边形中,向下、向下、返回,然后关闭这两个数组。

#2


12  

Personally, I find it both elegant and convenient to wrap the fill function. To fill between two equally sized row vectors Y1 and Y2 that share the support X (and color C):

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

fill_between_lines = @(X,Y1,Y2,C) fill( [X fliplr(X)],  [Y1 fliplr(Y2)], C );

#3


11  

You can accomplish this using the function FILL to create filled polygons under the sections of your plots. You will want to plot the lines and polygons in the order you want them to be stacked on the screen, starting with the bottom-most one. Here's an example with some sample data:

您可以使用函数填充来完成这一任务,在您的绘图区域中创建填充的多边形。你将会想要把线和多边形按你想要的顺序排列在屏幕上,从最底部开始。下面是一些示例数据的示例:

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

And here's the resulting figure:

结果是这样的:

在MATLAB中,填充两组数据之间的区域,一个图形中的线条。

You can also change the stacking order of the objects in the figure after you've plotted them by modifying the order of handles in the 'Children' property of the axes object. For example, this code reverses the stacking order, hiding the green polygon behind the blue polygon:

您还可以更改图中对象的堆叠顺序,通过修改坐标轴对象的“Children”属性中的句柄顺序。例如,这段代码颠倒了堆栈顺序,隐藏了蓝色多边形后面的绿色多边形:

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

Finally, if you don't know exactly what order you want to stack your polygons ahead of time (i.e. either one could be the smaller polygon, which you probably want on top), then you could adjust the 'FaceAlpha' property so that one or both polygons will appear partially transparent and show the other beneath it. For example, the following will make the green polygon partially transparent:

最后,如果你不知道你想堆栈顺序提前多边形(即任何一个可能是较小的多边形,你可能想要在上面),然后你就可以调整“FaceAlpha”属性,这样就会出现一个或两个多边形部分透明并显示下面的其他。例如,下面将使绿色多边形部分透明:

set(h2,'FaceAlpha',0.5);

#4


4  

You want to look at the patch() function, and sneak in points for the start and end of the horizontal line:

您需要查看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]);

If you only want the filled in area for a part of the data, you'll need to truncate the x and y vectors to only include the points you need.

如果您只希望填充部分数据,那么您需要截断x和y向量,只包含您需要的点。

#1


49  

Building off of @gnovice's answer, you can actually create filled plots with shading only in the area between the two curves. Just use fill in conjunction with fliplr.

通过@ gnew的回答,你可以创建一个只在两个曲线之间的区域的填充图。只需要和fliplr一起使用。

Example:

例子:

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

在MATLAB中,填充两组数据之间的区域,一个图形中的线条。

By flipping the x array and concatenating it with the original, you're going out, down, back, and then up to close both arrays in a complete, many-many-many-sided polygon.

通过翻转x数组并将其与原来的数组连接起来,您将会在一个完整的、多用途的多边形中,向下、向下、返回,然后关闭这两个数组。

#2


12  

Personally, I find it both elegant and convenient to wrap the fill function. To fill between two equally sized row vectors Y1 and Y2 that share the support X (and color C):

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

fill_between_lines = @(X,Y1,Y2,C) fill( [X fliplr(X)],  [Y1 fliplr(Y2)], C );

#3


11  

You can accomplish this using the function FILL to create filled polygons under the sections of your plots. You will want to plot the lines and polygons in the order you want them to be stacked on the screen, starting with the bottom-most one. Here's an example with some sample data:

您可以使用函数填充来完成这一任务,在您的绘图区域中创建填充的多边形。你将会想要把线和多边形按你想要的顺序排列在屏幕上,从最底部开始。下面是一些示例数据的示例:

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

And here's the resulting figure:

结果是这样的:

在MATLAB中,填充两组数据之间的区域,一个图形中的线条。

You can also change the stacking order of the objects in the figure after you've plotted them by modifying the order of handles in the 'Children' property of the axes object. For example, this code reverses the stacking order, hiding the green polygon behind the blue polygon:

您还可以更改图中对象的堆叠顺序,通过修改坐标轴对象的“Children”属性中的句柄顺序。例如,这段代码颠倒了堆栈顺序,隐藏了蓝色多边形后面的绿色多边形:

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

Finally, if you don't know exactly what order you want to stack your polygons ahead of time (i.e. either one could be the smaller polygon, which you probably want on top), then you could adjust the 'FaceAlpha' property so that one or both polygons will appear partially transparent and show the other beneath it. For example, the following will make the green polygon partially transparent:

最后,如果你不知道你想堆栈顺序提前多边形(即任何一个可能是较小的多边形,你可能想要在上面),然后你就可以调整“FaceAlpha”属性,这样就会出现一个或两个多边形部分透明并显示下面的其他。例如,下面将使绿色多边形部分透明:

set(h2,'FaceAlpha',0.5);

#4


4  

You want to look at the patch() function, and sneak in points for the start and end of the horizontal line:

您需要查看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]);

If you only want the filled in area for a part of the data, you'll need to truncate the x and y vectors to only include the points you need.

如果您只希望填充部分数据,那么您需要截断x和y向量,只包含您需要的点。