Python Matplotlib,从三个不等长的数组创建堆叠直方图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18449602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:48:09  来源:igfitidea点击:

Matplotlib, creating stacked histogram from three unequal length arrays

pythonmatplotlib

提问by ncRubert

I'd like to create a stacked histogram. If I have a single 2-D array, made of three equal length data sets, this is simple. Code and image below:

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

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, normed = True)
plt.show()

enter image description here

在此处输入图片说明

However, if I try similar code with three data sets of a different length the results are that one histogram covers up another. Is there any way I can do the stacked histogram with mixed length data sets?

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

##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, normed = True)
plt.hist(x2, bins, stacked=True, normed = True)
plt.hist(x3, bins, stacked=True, normed = True)
plt.show()

enter image description here

在此处输入图片说明

采纳答案by ncRubert

Well, this is simple. I just need to put the three arrays in a list.

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

##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()