【问题标题】:How to stack multiple histograms in a single figure in Python?如何在 Python 中将多个直方图堆叠在一个图形中?
【发布时间】:2020-05-11 07:07:57
【问题描述】:

我有一个形状为 [30, 10000] 的 numpy 数组,其中第一个轴是时间步长,第二个轴包含观察到的一系列 10000 个变量的值。我想在一个图中可视化数据,类似于:

你可以在 seaborn 教程here 中找到。基本上,我想要的是为 30 个时间步骤中的每一个绘制一个包含 30/40 个 bin 的直方图,然后 - 以某种方式 - 将这些直方图连接起来以具有一个共同的轴并将它们绘制在同一个图中。

我的数据看起来像一个随时间移动并变宽的高斯函数。您可以使用以下代码重现类似的内容:

mean = 0.0
std = 1.0

data = []
for t in range(30):
    mean = mean + 0.01
    std = std + 0.1
    data.append(np.random.normal(loc=mean, scale=std, size=[10000]))

data = np.array(data)

与上图类似的图最好,但感谢任何帮助!

谢谢你, G.

【问题讨论】:

    标签: python matplotlib histogram seaborn figure


    【解决方案1】:

    使用直方图?你可以用 np.hist2d 做到这一点,但这种方式更清晰一些......

    import matplotlib.pyplot as plt
    import numpy as np
    
    data = np.random.randn(30, 10000)
    
    H = np.zeros((30, 40))
    bins = np.linspace(-3, 3, 41)
    for i in range(30):
        H[i, :], _ = np.histogram(data[i, :], bins)
    fig, ax = plt.subplots()
    times = np.arange(30) * 0.1
    pc = ax.pcolormesh(bins, times, H)
    ax.set_xlabel('data bins')
    ax.set_ylabel('time [s]')
    fig.colorbar(pc, label='count')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-13
      • 2017-08-26
      • 2015-06-23
      • 2020-09-26
      • 1970-01-01
      • 2017-10-25
      • 2022-08-12
      • 2017-02-05
      相关资源
      最近更新 更多