在Matplotlib中,该参数在fig.add_subplot(111)中意味着什么?

巧了我就是萌 提交于 2020-02-28 03:52:35

有时我遇到这样的代码:

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()的参数是什么意思?


#1楼

我认为最好用以下图片解释:

要初始化以上内容,请输入:

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

#2楼

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

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


#3楼

我的解决方案是

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


#4楼

fig.add_subplot(ROW,COLUMN,POSITION)

  • 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个子图。 它的位置是第一。 一共有2行,一列,因此可以绘制2个子图。其位置为第2个


#5楼

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)表示第二列只有两行,位置参数逐行移动。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!