【问题标题】:Matplolib subplots what is the purpose of the fig and axs variablesMatplotlib subplots fig 和 axs 变量的用途是什么
【发布时间】:2020-02-22 00:25:38
【问题描述】:

考虑以下代码:

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)
data = np.random.randn(2, 100)

fig, axs = plt.subplots(2, 2, figsize=(5, 5))
axs[0, 0].hist(data[0])
axs[1, 0].scatter(data[0], data[1])
axs[0, 1].plot(data[0], data[1])
axs[1, 1].hist2d(data[0], data[1])

plt.show()

我知道为了创建子图,您需要编写以下代码行:

fig, axs = plt.subplots(2, 2, figsize=(5, 5)

但是我的问题是关于这一行的含义,例如它通过生成变量 fig 和 axs 实际实现了什么,以及为什么稍后我们使用 ax[0,0] 而不是 fig[0,0]

【问题讨论】:

  • 一个图包含4个轴?

标签: matplotlib histogram subplot fig


【解决方案1】:

fig 描述了整个图形,但在这种情况下,axs 指的是图形内的所有子图。由于您定义了 2 行和 2 列子图,因此您将每个子图称为左上角的 axs[0,0] 和右下角的 axs[1,1]。为了更改子图的大小,您必须更改嵌入子图的整体图形的大小。

差异是细微的,但可以在图中找到多个子图或仅一个子图。因此,要绘制一条线,您可以在子图轴上而不是在图形上执行此操作。

【讨论】:

    猜你喜欢
    • 2011-04-01
    • 2018-05-17
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 2015-09-12
    • 1970-01-01
    相关资源
    最近更新 更多