在Matplotlib中,参数在图add_subblot(111)中是什么意思?

2024-05-08 23:02:25 发布

您现在位置:Python中文网/ 问答频道 /正文

有时我遇到这样的代码:

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


Tags: 代码文档importadd参数matplotlibasshow
3条回答

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

在Matlab文档的Figure Setup - Displaying Multiple Plots per Figure部分中解释了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.

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

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

我想这最好用下面的图片来解释:

enter image description here

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

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

相关问题 更多 >