2016-11-17 4 views
2

だから、これを行うための一つの方法は、このコードを使用している私は、正規分布をプロットしたい、と私は見てきました:隠すヒストグラムプロット

import numpy as np 
import matplotlib.pyplot as plt 

mu = 5 
sigma = 1 

s = np.random.normal(mu, sigma, 1000) 

count, bins, ignored = plt.hist(s, 100, normed=True); 
pdf = 1/(sigma * np.sqrt(2 * np.pi)) * np.exp(- (bins - mu)**2/(2 * sigma**2)) 

mu_ = 10 
sigma_ = 1 
s = np.random.normal(mu_, sigma_, 1000) 

count_, bins_, ignored_ = plt.hist(s, 100, normed=True); 
pdf_ = 1/(sigma_ * np.sqrt(2 * np.pi)) * np.exp(- (bins_ - mu_)**2/(2 * sigma_**2)) 

plt.plot(bins, pdf, linewidth=2, color='g') 
plt.plot(bins_, pdf_, linewidth=2, color='r') 

plt.show() 

そして結果は次のとおりです。

enter image description here

私の質問は、何とか正規分布線だけが表示されるようにヒストグラムプロットを隠すことができますか?私は正規分布をプロットする別の方法があることを知っていますが、私はちょっとこの方法が好きです

ありがとうございました!まだあなたはそれが描画されてからの出力を使用することを可能にしながらこれは、ヒストグラムをクリアします

plt.plot(bins, pdf, linewidth=2, color='g') 
plt.plot(bins_, pdf_, linewidth=2, color='r') 

:右の前plt.clf()を追加

答えて

0

してみてください。 2つの別々の図形を持たせたい場合は、ヒストグラムを1つ、線分を1つにするには、plt.clf()の代わりにplt.figure()を追加します。

+1

、おかげでたくさんの男! – KaraiKare

0

リンゴのスライスを得る1つの方法は、もちろんリンゴパイを準備し、後でパイからすべてのリンゴを選ぶことです。簡単な方法は、確かにケーキを作ることではないでしょう。

したがって、図でヒストグラムプロットを持たない明らかな方法は、まずそれをプロットすることではありません。代わりに、numpy.histogram(とにかくfunction called by plt.hist)を使用してヒストグラムを計算し、その出力を図にプロットします。

 
import numpy as np 
import matplotlib.pyplot as plt 

mu = 5 
sigma = 1 

s = np.random.normal(mu, sigma, 1000) 

count, bins = np.histogram(s, 100, normed=True) 
pdf = 1/(sigma * np.sqrt(2 * np.pi)) * np.exp(- (bins - mu)**2/(2 * sigma**2)) 

mu_ = 10 
sigma_ = 1 
s = np.random.normal(mu_, sigma_, 1000) 

count_, bins_ = np.histogram(s, 100, normed=True) 
pdf_ = 1/(sigma_ * np.sqrt(2 * np.pi)) * np.exp(- (bins_ - mu_)**2/(2 * sigma_**2)) 

plt.plot(bins, pdf, linewidth=2, color='g') 
plt.plot(bins_, pdf_, linewidth=2, color='r') 

plt.show() 
それが動作

enter image description here

関連する問題