如何在Python中的单个图形中堆叠多个直方图?

时间:2020-01-25 00:09:51

标签: python matplotlib histogram seaborn figure

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

您可以在最新的教程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。

1 个答案:

答案 0 :(得分:3)

使用直方图?您可以使用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')

enter image description here