2013-08-26 9 views
29

スタックヒストグラムを作成したいと思います。 3つの等しい長さのデータセットからなる1つの2次元配列があれば、これは簡単です。以下のコードと画像:Matplotlib、3つの異なる長さの配列から積み重ねられたヒストグラムを作成する

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

私は長さの異なる3つのデータセットと同様のコードをしようとした場合しかし、結果は1つのヒストグラムは別のをカバーすることです。混合長データセットを使用して積み上げヒストグラムを実行する方法はありますか?

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

答えて

48

まあ、これは簡単です。 3つの配列をリストに入れるだけです。

##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」、および「x3」の凡例を個別に追加するにはどうすればよいですか? – Boern

+0

plt.hist([x1、x2、x3]、bins、stacked = True、color = ["red"、 "blue"、 "violet"]、normed = True); plt.legend({label1: "red"、label2: "blue"、label3: "violet"}) –

関連する問題