Python 在 Matplotlib 中,fig.add_subplot(111) 中的参数是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3584805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:50:05  来源:igfitidea点击:

In Matplotlib, what does the argument mean in fig.add_subplot(111)?

pythonmatplotlibfigure

提问by pleasedontbelong

Sometimes I come across code such as this:

有时我会遇到这样的代码:

import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
fig = plt.figure()
fig.add_subplot(111)
plt.scatter(x, y)
plt.show()

Which produces:

其中产生:

Example plot produced by the included code

包含的代码生成的示例图

I've been reading the documentation like crazy but I can't find an explanation for the 111. sometimes I see a 212.

我一直在疯狂地阅读文档,但找不到111. 有时我看到一个212.

What does the argument of fig.add_subplot()mean?

的论证fig.add_subplot()是什么意思?

采纳答案by Constantin

These are subplot grid parameters encoded as a single integer. For example, "111" means "1x1 grid, first subplot" and "234" means "2x3 grid, 4th subplot".

这些是编码为单个整数的子图网格参数。例如,“111”表示“1x1 网格,第一个子图”,“234”表示“2x3 网格,第四个子图”。

Alternative form for add_subplot(111)is add_subplot(1, 1, 1).

的替代形式add_subplot(111)add_subplot(1, 1, 1)

回答by DaveTM

The answer from Constantin is spot on but for more background this behavior is inherited from Matlab.

康斯坦丁的答案是正确的,但对于更多背景,这种行为是从 Matlab 继承的。

The Matlab behavior is explained in the Figure Setup - Displaying Multiple Plots per Figuresection of the Matlab documentation.

Matlab 行为在 Matlab 文档的Figure Setup - Displaying Multiple Plots per Figure部分中进行了解释。

subplot(m,n,i) breaks the figure window into an m-by-n matrix of small subplots and selects the ithe subplot for the current plot. The plots are numbered along the top row of the figure window, then the second row, and so forth.

subplot(m,n,i) 将图形窗口分解为一个由小子图组成的 m×n 矩阵,并为当前图选择子图。这些图沿着图形窗口的顶行编号,然后是第二行,依此类推。

回答by SaiyanGirl

I think this would be best explained by the following picture:

我认为这最好通过下图来解释:

enter image description here

在此处输入图片说明

To initialize the above, one would type:

要初始化上述内容,可以键入:

import matplotlib.pyplot as plt
fig = plt.figure()
fig.add_subplot(221)   #top left
fig.add_subplot(222)   #top right
fig.add_subplot(223)   #bottom left
fig.add_subplot(224)   #bottom right 
plt.show()

回答by yoonghm

My solution is

我的解决方案是

fig = plt.figure()
fig.add_subplot(1, 2, 1)   #top and bottom left
fig.add_subplot(2, 2, 2)   #top right
fig.add_subplot(2, 2, 4)   #bottom right 
plt.show()

2x2 grid with 1 and 3 merge

1 和 3 合并的 2x2 网格

回答by Rameez Ahmad Dar

fig.add_subplot(ROW,COLUMN,POSITION)

fig.add_subplot(ROW,COLUMN,POSITION)

  • ROW=number of rows
  • COLUMN=number of columns
  • POSITION= position of the graph you are plotting
  • ROW=行数
  • COLUMN=列数
  • POSITION=您正在绘制的图形的位置

Examples

例子

`fig.add_subplot(111)` #There is only one subplot or graph  
`fig.add_subplot(211)`  *and*  `fig.add_subplot(212)` 

There are total 2 rows,1 column therefore 2 subgraphs can be plotted. Its location is 1st. There are total 2 rows,1 column therefore 2 subgraphs can be plotted.Its location is 2nd

总共有 2 行,1 列,因此可以绘制 2 个子图。它的位置是第一。总共有 2 行,1 列,因此可以绘制 2 个子图。它的位置是第 2

回答by Pritz

enter image description here

在此处输入图片说明

import matplotlib.pyplot as plt
plt.figure(figsize=(8,8))
plt.subplot(3,2,1)
plt.subplot(3,2,3)
plt.subplot(3,2,5)
plt.subplot(2,2,2)
plt.subplot(2,2,4)

The first code creates the first subplot in a layout that has 3 rows and 2 columns.

第一个代码在具有 3 行 2 列的布局中创建第一个子图。

The three graphs in the first column denote the 3 rows. The second plot comes just below the first plot in the same column and so on.

第一列中的三个图形表示 3 行。第二个图位于同一列中的第一个图下方,依此类推。

The last two plots have arguments (2, 2)denoting that the second column has only two rows, the position parameters move row wise.

最后两个图有参数(2, 2)表示第二列只有两行,位置参数按行移动。

回答by compuphys

The add_subplot()method has several call signatures:

add_subplot()方法有几个调用签名:

  1. add_subplot(nrows, ncols, index, **kwargs)
  2. add_subplot(pos, **kwargs)
  3. add_subplot(ax)
  4. add_subplot()<-- since 3.1.0
  1. add_subplot(nrows, ncols, index, **kwargs)
  2. add_subplot(pos, **kwargs)
  3. add_subplot(ax)
  4. add_subplot()<-- 从 3.1.0 开始


Calls 1 and 2:

调用 1 和 2:

Calls 1 and 2 achieve the same thing as one another (up to a limit, explained below). Think of them as first specifying the grid layout with their first 2 numbers(2x2, 1x8, 3x4, etc), e.g:

调用 1 和调用 2 彼此实现相同的目的(达到限制,如下所述)。将它们视为首先使用前 2 个数字(2x2、1x8、3x4 等)指定网格布局,例如:

f.add_subplot(3,4,1) 
# is equivalent to:
f.add_subplot(341)

Both produce a subplot arrangement of (3 x 4 = 12) subplots in 3 rows and 4 columns. The third numberin each call indicates which axis object to return, starting from 1 at the top left, increasing to the right.

两者都产生一个由 (3 x 4 = 12) 个 3 行 4 列子图组成的子图排列。每次调用中的第三个数字表示要返回的轴对象,从左上角的 1开始,向右增加

This code illustrates the limitations of using call 2:

此代码说明了使用调用 2 的限制:

#!/usr/bin/env python3
import matplotlib.pyplot as plt

def plot_and_text(axis, text):
  '''Simple function to add a straight line
  and text to an axis object'''
  axis.plot([0,1],[0,1])
  axis.text(0.02, 0.9, text)

f = plt.figure()
f2 = plt.figure()

_max = 12
for i in range(_max):
  axis = f.add_subplot(3,4,i+1, fc=(0,0,0,i/(_max*2)), xticks=[], yticks=[])
  plot_and_text(axis,chr(i+97) + ') ' + '3,4,' +str(i+1))

  # If this check isn't in place, a 
  # ValueError: num must be 1 <= num <= 15, not 0 is raised
  if i < 9:
    axis = f2.add_subplot(341+i, fc=(0,0,0,i/(_max*2)), xticks=[], yticks=[])
    plot_and_text(axis,chr(i+97) + ') ' + str(341+i))

f.tight_layout()
f2.tight_layout()
plt.show()

subplots

子图

You can see with call 1 on the LHSyou can return any axis object, however with call 2 on the RHSyou can only return up to index = 9 rendering subplots j), k), and l) inaccessible using this call.

您可以看到LHS 上的调用 1可以返回任何轴对象,但是RHS 上的调用 2最多只能返回 index = 9 渲染子图 j)、k) 和 l) 使用此调用无法访问。

I.e it illustrates this point from the documentation:

即它从文档中说明了这一点

pos is a three digit integer, where the first digit is the number of rows, the second the number of columns, and the third the index of the subplot. i.e. fig.add_subplot(235) is the same as fig.add_subplot(2, 3, 5). Note that all integers must be less than 10 for this form to work.

pos 是一个三位整数,其中第一位是行数,第二位是列数,第三位是子图的索引。即 fig.add_subplot(235) 与 fig.add_subplot(2, 3, 5) 相同。请注意,所有整数必须小于 10 才能使用此表单



Call 3

呼叫 3

In rare circumstances, add_subplot may be called with a single argument, a subplot axes instance already created in the present figure but not in the figure's list of axes.

在极少数情况下,可以使用单个参数调用 add_subplot,子图轴实例已在当前图形中创建,但不在图形的轴列表中。



Call 4 (since 3.1.0):

调用 4(自 3.1.0 起):

If no positional arguments are passed, defaults to (1, 1, 1).

如果没有传递位置参数,则默认为 (1, 1, 1)。

i.e., reproducing the call fig.add_subplot(111)in the question.

即,重现fig.add_subplot(111)问题中的调用。