【问题标题】:Placing multiple histograms in a stack with matplotlib使用 matplotlib 将多个直方图放在一个堆栈中
【发布时间】:2020-06-11 20:52:28
【问题描述】:

我正在尝试将多个直方图放在垂直堆栈中。我能够在堆栈中获取图,但是所有直方图都在同一个图中。

fig, ax = plt.subplots(2, 1, sharex=True, figsize=(20, 18))

n = 2

axes = ax.flatten()
for i, j in zip(range(n), axes):
    plt.hist(np.array(dates[i]), bins=20, alpha=0.3)


plt.show()

【问题讨论】:

  • for i, ax in zip(range(n), axes):, ax.hist(....).

标签: python matplotlib histogram


【解决方案1】:

您有一个 2x1 的轴对象网格。通过直接循环 axes.flatten(),您一次访问一个子图。然后您需要使用相应的轴实例来绘制直方图。

fig, axes = plt.subplots(2, 1, sharex=True, figsize=(20, 18)) # axes here

n = 2

for i, ax in zip(range(n), axes.flatten()): # <--- flatten directly in the zip
    ax.hist(np.array(dates[i]), bins=20, alpha=0.3)

您的原始版本也是正确的,只是您应该使用正确的变量而不是 plt,而不是 j,而不是 plt

for i, j in zip(range(n), axes):
    j.hist(np.array(dates[i]), bins=20, alpha=0.3)

【讨论】:

    猜你喜欢
    • 2016-08-13
    • 2023-03-18
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 2018-03-14
    • 2014-03-12
    • 2013-12-26
    相关资源
    最近更新 更多