本文介绍了subplot 是如何工作的,MATLAB 中的 subplot(121) 和 subplot(1,2,1) 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 subplot 的工作原理有点不清楚.具体来说,MATLAB 中 subplot(121)subplot(1,2,1) 的区别是什么?我试图搜索 subplot 文档,但似乎找不到我要找的东西.

I am a bit unclear with how subplot works. Specifically, what is the difference between subplot(121) and subplot(1,2,1) in MATLAB? I have tried to search the subplot documentation, but I can't seem to find what I am looking for.

推荐答案

长话短说,没有区别.subplot 的工作原理如下:

Long story short, there is no difference. How subplot works is the following:

subplot(m,n,p); %//or
subplot(mnp);

您在 subplot 中使用了三个数字.subplot 在同一个窗口中放置多个图形.您可以将绘图放置在 m x n 网格中,其中 m 包含行数,n 包含图形中的列数.p 决定了您希望将绘图放置在网格内的位置.数字p1增加到m x n,图从左到右,从上到下排列.

You have three numbers that are used within subplot. subplot places multiple figures within the same window. You can place plots within a m x n grid, where m contains the number of rows and n contains the number of columns in your figure. p determines where you want to place your plot within the grid. The number p increases from 1 up to m x n, and the plots are placed from left to right, and top to bottom.

在这种情况下,当您执行 subplot(1,2,1);subplot(121); 时,您希望有 one 行和 列的数字.最后一个数字 p=1 表示您希望将绘图放置在 最左侧 列中.当您执行 subplot(1,2,2);subplot(122); 时,这是 p=2 并且您希望将绘图放在最右侧列中.

In this case, when you do subplot(1,2,1); or subplot(121);, you would like to have one row and two columns worth of figures. The last number, p=1 means that you wish to place the plot in the left most column. When you do subplot(1,2,2); or subplot(122);, this is when p=2 and you wish to place the plot in the right most column.

您如何使用 subplot 的方式如下:

How you use subplot is in the following fashion:

  1. 首先确定在此窗口中需要多少行和列的绘图(即 mn).
  2. 生成一个空白的figure窗口
  3. 对于您要创建的每个图...
    • 调用 subplot 并选择您希望绘图出现的正确位置.
    • 编写必要的代码来创建您的绘图,就像创建一个占据单个窗口的绘图一样.
    • 绘制您的数据
  1. Determine how many rows and columns of plots you want within this window first (i.e. m and n).
  2. Spawn a blank figure window
  3. For each plot you want to create...
    • Call subplot and choose the right location(s) of where you want the plot to appear.
    • Write the necessary code to create your plot like you would for just a plot occupying a single window.
    • Plot your data

这是一个说明性的例子.让我们创建一个窗口,在同一窗口中具有行和列的数字.因此:

Here is an illustrative example. Let's create a window that has two rows and three columns worth of figures within the same window. As such:

figure;
rng(10); %// Set seed for reproducibility
subplot(2,3,1);
x = rand(100,1);
y = rand(100,1);
plot(x,y,'b.');
title('First plot');
subplot(2,3,2);
x = rand(100,1);
y = rand(100,1);
plot(x,y,'b.');
title('Second plot');
subplot(2,3,3);
x = rand(100,1);
y = rand(100,1);
plot(x,y,'b.');
title('Third plot');
subplot(2,3,4);
x = rand(100,1);
y = rand(100,1);
plot(x,y,'b.');
title('Fourth plot');
subplot(2,3,5);
x = rand(100,1);
y = rand(100,1);
plot(x,y,'b.');
title('Fifth plot');
subplot(2,3,6);
x = rand(100,1);
y = rand(100,1);
plot(x,y,'b.');
title('Sixth plot');

以上代码的作用是我们为 xy 对生成 100 x 1 的随机点集,并且我们将它们绘制在整个窗口内的多个位置.请注意,subplot 的最后一个参数线性增加,而前两个参数保持不变.在开始绘图之前,必须确保知道整个窗口内需要多少个数字.上面代码描述的图如下所示:

What the above code does is that we generate random sets of points that are 100 x 1 each for pairs of x and y and we plot them in multiple locations within the overall window. Notice that the last parameter of subplot increases linearly, while the first two parameters stay the same. You must make sure that you know how many figures you want within the overall window before you start plotting. The figure that the code above describes looks like the following:

您还可以为 p 指定点的向量.然而,如果你这样做,你必须这样调用subplot:subplot(m,n,p);.如果 p单个数字,则 subplot(m,n,p);subplot(mnp) 会起作用.

You can also specify a vector of points for p. However, should you do it this way, you must call subplot this way: subplot(m,n,p);. If p is a single number, then either subplot(m,n,p); or subplot(mnp) will work.

如果您将 p 指定为 vector,这将导致您绘制的一个图将占据其中的多个空间/槽相同的图形窗口.例如,如果您执行了:subplot(2,3,1:3);,这将采用一个图并占据图形的整个第一行.然后,您可以在插槽 4、5 和 6 中发布更多图.换句话说:

If you specify p to be a vector, what this will do is that one plot you make will occupy multiple spaces / slots within the same figure window. As an example, if you did: subplot(2,3,1:3);, this will take one plot and occupy the entire first row of your figure. You can then issue more plots in slots 4, 5 and 6. In other words:

figure;
rng(10); %// Set seed for reproducibility
subplot(2,3,1:3);
x = rand(100,1);
y = rand(100,1);
plot(x,y,'b.');
title('First plot');
subplot(2,3,4:5);
x = rand(100,1);
y = rand(100,1);
plot(x,y,'b.');
title('Second plot');
subplot(2,3,6);
x = rand(100,1);
y = rand(100,1);
plot(x,y,'b.');
title('Third plot');

如图所示:

如您所见,我们使用 subplot(2,3,1:3); 和第一个图占据了第一行.第二个图使用 subplot(2,3,4:5); 占用插槽 p=4,p=5.这占据了第二行、第一列和第二列.最后,我们的最后一个图使用 subplot(2,3,6); 占据第二行第三列.请记住,插槽从左到右,从上到下,p 不仅可以是单个数字,也可以是向量.如果你想占据前两行两列,你可以做 subplot(2,3,[1 2 4 5]); 现在,如果你想占据整个最右边的一列,你可以做subplot(2,3,[3 6]);,或者如果你只想最右边一列的最上面的位置,你可以做subplot(2,3,3);subplot(233);,那么如果你想处理最后一列和右下角的最后一个位置,你可以这样做subplot(2,3,6);subplot(236);

As you can see, we have occupied the first row using subplot(2,3,1:3); with the first plot. The second plot occupies slots p=4,p=5 using subplot(2,3,4:5);. This occupies the second row, and first and second columns. Finally our last plot occupies the second row, third column using subplot(2,3,6);. Remember, the slots go from left to right and top to bottom, and p can not only be a single number but a vector as well. If you wanted to occupy the first two rows and two columns, you would do subplot(2,3,[1 2 4 5]); Now, if you wanted to occupy the entire right most column, you can do subplot(2,3,[3 6]);, or if you just want the top most location in the right most column, you can do subplot(2,3,3); or subplot(233);, then if you want to tackle the last location in the last column and at the bottom right, you can do subplot(2,3,6); or subplot(236);

我要确保您记住的最后一件事是,您需要确保在决定显示您的情节之前调用 subplot .完成后,切换到下一个位置并继续工作.

One final thing that I want to make sure that you remember is that you need to make sure you call subplot before you decide to show your plot. Once you're finished, switch over to the next slot and keep working.

希望这有帮助!祝你好运!

Hope this helps! Good luck!

这篇关于subplot 是如何工作的,MATLAB 中的 subplot(121) 和 subplot(1,2,1) 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 04:32