gpt4 book ai didi

python - 从不等长度的数组创建堆叠直方图

转载 作者:IT老高 更新时间:2023-10-28 20:35:24 27 4
gpt4 key购买 nike

我想创建一个堆叠直方图。如果我有一个由三个等长数据集组成的二维数组,这很简单。代码和图片如下:

import numpy as np
from matplotlib import pyplot as plt

# create 3 data sets with 1,000 samples
mu, sigma = 200, 25
x = mu + sigma*np.random.randn(1000,3)

#Stack the data
plt.figure()
n, bins, patches = plt.hist(x, 30, stacked=True, density = True)
plt.show()

enter image description here

但是,如果我尝试使用具有不同长度的三个数据集的类似代码,结果是一个直方图覆盖了另一个。有什么办法可以用混合长度的数据集做堆叠直方图?

##Continued from above
###Now as three separate arrays
x1 = mu + sigma*np.random.randn(990,1)
x2 = mu + sigma*np.random.randn(980,1)
x3 = mu + sigma*np.random.randn(1000,1)

#Stack the data
plt.figure()
plt.hist(x1, bins, stacked=True, density = True)
plt.hist(x2, bins, stacked=True, density = True)
plt.hist(x3, bins, stacked=True, density = True)
plt.show()

enter image description here

最佳答案

嗯,这很简单。我只需要将三个数组放在一个列表中。

##Continued from above
###Now as three separate arrays
x1 = mu + sigma*np.random.randn(990,1)
x2 = mu + sigma*np.random.randn(980,1)
x3 = mu + sigma*np.random.randn(1000,1)

#Stack the data
plt.figure()
plt.hist([x1,x2,x3], bins, stacked=True, density=True)
plt.show()

关于python - 从不等长度的数组创建堆叠直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18449602/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com