关于python:在Matplotlib中,参数在fig.add_subplot(111)中意味着什么?

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

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

1
2
3
4
5
6
7
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()

产生:

Example plot produced by the included code

我一直在疯狂地阅读文档,但我找不到对111的解释。有时我会看到一个212

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


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

enter image description here

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

1
2
3
4
5
6
7
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()


这些是编码为单个整数的子块网格参数。例如,"111"表示"1X1网格,第一子批次","234"表示"2X3网格,第四子批次"。

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


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

matlab的行为在matlab文档的"图形设置-每个图形显示多个图形"部分中进行了说明。

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.


我的解决方案是

1
2
3
4
5
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()

氧化镁


埃多克斯1〔2〕

  • 行=行数
  • 列=列数
  • 位置=正在绘制的图形的位置

示例

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

共有2行,1列,因此可以绘制2个子图。其位置为1。共有2行,1列,因此可绘制2个子图。其位置为2


氧化镁

1
2
3
4
5
6
7
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)

第一个代码创建布局中的第一个子批次,该布局有3行和2列。

第一列中的三个图表示三行。第二个图正好位于同一列中第一个图的下方,以此类推。

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