2013-08-26 17 views
29

Yığınlanmış bir histogram oluşturmak istiyorum. Üç eşit uzunlukta veri kümesinden oluşan tek bir 2-D dizilim varsa, bu basittir. Ben farklı uzunlukta üç veri setleri ile benzer bir kod deneyinMatplotlib, üç eşitsiz uzunluk dizisinden yığılmış histogram oluşturma

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

Ancak, sonuçlar bir histogram başka yukarı kapsadığını şunlardır: Aşağıdaki kodu ve görüntü. Yığılmış histogramı karışık uzunluktaki veri kümeleriyle yapabileceğim herhangi bir yol var mı?

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

cevap

48

Eh, bu basittir. Sadece üç diziyi bir listeye koymam gerek.

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

x1', 'x2' ve' x3' için ayrı bir efsane eklemenin en iyi yolu nedir? – Boern

+0

plt.hist ([x1, x2, x3], kutuları, yığılmış = Doğru, renk = ["kırmızı", "mavi", "menekşe"], normed = Doğru); plt.legend ({label1: "red", label2: "blue", label3: "violet"}) –