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

问题描述 投票:2回答:1

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

您可以在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个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.