2017-10-23 4 views
0

Pythonでは、分布のモデルの密度のパラメータを推定し、分布のヒストグラムの上に密度関数をプロットしたいと思います。 Rでは、オプションprop=TRUEを使用するのと同様です。ヒストグラムの上に密度関数をプロットする

import numpy as np 
import matplotlib.mlab as mlab 
import matplotlib.pyplot as plt 

# initialization of the list "data" 
# estimation of the parameter, in my case, mean and variance of a normal distribution 

plt.hist(data, bins="auto") # data is the list of data 
# here I would like to draw the density above the histogram 
plt.show() 

私は最も厄介な部分は、それをフィットさせることです。

編集:私はこの最初の答えに応じて試してみました:

mean = np.mean(logdata) 
var = np.var(logdata) 
std = np.sqrt(var) # standard deviation, used by numpy as a replacement of the variance 
plt.hist(logdata, bins="auto", alpha=0.5, label="données empiriques") 
x = np.linspace(min(logdata), max(logdata), 100) 
plt.plot(x, mlab.normpdf(x, mean, std)) 
plt.xlabel("log(taille des fichiers)") 
plt.ylabel("nombre de fichiers") 
plt.legend(loc='upper right') 
plt.grid(True) 
plt.show() 

しかし、それはここでは、グラフに合わせて、それがどのように見えるかではありません。What I get with the python code above. I would like the density to fit the histogram but the values are too small.

**編集2 * *ヒストグラム機能のオプションnormed=Trueで動作します。 Something that would look like this (done in R with the optionprob=TRUE)

+0

を使用する必要があります。 Figureをどのように見せたいかに関するスクリーンショット/詳細情報を追加できますか?いつものように、[最小、完全、そして実証可能な例](http://stackoverflow.com/help/mcve)は、良い答えを得るチャンスを大いに助けます。 – DavidG

+0

ここにあります。はい、あなたは1000の説明よりも良い例が正しいです!しかし、まだ私は私の説明がかなり明確だと思った... –

+0

'normed = True'を使いたくない場合、私は解決策を追加しました。 – DavidG

答えて

0

私が正しく理解していると、いくつかのデータの平均と標準偏差があります。あなたはこれのヒストグラムをプロットし、ヒストグラムに正規分布線をプロットしたいと思います。この行はmatplotlib.mlab.normpdf()を使用して生成でき、ドキュメントはhereです。次の図を与える

import numpy as np 
import matplotlib.mlab as mlab 
import matplotlib.pyplot as plt 

mean = 100 
sigma = 5 

data = np.random.normal(mean,sigma,1000) # generate fake data 
x = np.linspace(min(data), max(data), 100) 

plt.hist(data, bins="auto",normed=True) 
plt.plot(x, mlab.normpdf(x, mean, sigma)) 

plt.show() 

enter image description here

編集:上記はnormed = Trueで動作します。これができない場合は、我々は我々自身の関数を定義することができます

def gauss_function(x, a, x0, sigma): 
    return a * np.exp(-(x - x0) ** 2/(2 * sigma ** 2)) 

mean = 100 
sigma = 5 

data = np.random.normal(mean,sigma,1000) # generate fake data 
x = np.linspace(min(data), max(data), 1000) 

test = gauss_function(x, max(data), mean, sigma) 

plt.hist(data, bins="auto") 
plt.plot(x, test) 

plt.show() 
0

あなたが探しているものすべてを、すでにseabornです。

あなたはそれはあなたが探しているものは明らかではありませんdistplot

import seaborn as sns 
import numpy as np 

data = np.random.normal(5, 2, size=1000) 
sns.distplot(data) 

plot is here

関連する問題