python - fig, ax = plt.subplots() 意思

标签 python matplotlib

我已经使用 matplotlib 一段时间了,但我并不真正理解这一行的作用。

fig, ax = plt.subplots()

谁能解释一下?

最佳答案

plt.subplots() 基本上是一个(非常好的)初始化图形和子图轴的快捷方式。 See the docs here .特别是,

>>> fig, ax = plt.subplots(1, 1)

本质上等同于

>>> fig = plt.figure()
>>> ax = fig.add_subplot(1, 1)

但是 plt.subplots() 对于一次构造多个轴最有用,例如,

>>> fig, axes = plt.subplots(2, 3)

制作一个2行3列子图的图形,本质上等同于

>>> fig = plt.figure()
>>> axes = np.empty((2,3))
>>> for i in range(2):
...     for j in range(3):
...         axes[i,j] = fig.add_subplot(2, 3, (i*j)+j+1)

我说“本质上”是因为 plt.subplots() 也有一些不错的功能,比如 sharex=True 强制每个子图共享相同的 x 轴(即,相同的轴限制/比例等)。这是我最喜欢的初始化图窗的方法,因为它为您提供了图窗和所有轴在一条平滑线上的 handle 。

关于python - fig, ax = plt.subplots() 意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63039065/

相关文章:

python - NAN 值在 python 中被视为字符串

python - Numpy 导入失败

python - 讲解python模块itertools的组合函数

python - 如何设置 NetworkX 边缘标签偏移量? (避免标签重叠)

python - matplotlib 在自动生成的 html 中嵌入图形

python-3.x - seaborn histplot 和 displot 输出不匹配

python - matplotlib 散点图,其中 xyz 轴线穿过原点 (0,0,0),轴投影线到每个点

python - 仅包含 np 数组的一列上的 Pandas 相关性

python - 比较 2 个不同的列表列表

python - 在轮廓上绘制点 - Matplotlib/Python