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

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()

生产:

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

fig.add_subplot()的参数是什么意思?


当前回答

fig.add_subplot(行、列、位置)

ROW=行数 COLUMN=列数 POSITION=所绘制图形的位置

例子

`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号

其他回答

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列的布局中创建了第一个子图。

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

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

fig.add_subplot(行、列、位置)

ROW=行数 COLUMN=列数 POSITION=所绘制图形的位置

例子

`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号

我的解决方案是

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()

Fig.add_subplot(111)与Fig.add_subplot(1,1,1)一样,111只是subplot网格参数,只是编码为单个整数。

要选择n*m网格中的第k个子图,你可以这样做:fig.add_subplot(n, m, k)。

我认为这可以用下面的图片来解释:

要初始化上面的代码,可以输入:

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()