在直方图上绘制密度函数

时间:2017-10-23 12:18:38

标签: python matplotlib statistics

在Python中,我估计了我的分布模型密度的参数,我想在分布的直方图上方绘制密度函数。在R中,它类似于使用选项UILabel

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

但它不适合图表,以下是它的外观:What I get with the python code above. I would like the density to fit the histogram but the values are too small.

**编辑2 **在直方图功能中使用选项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() Something that would look like this (done in R with the optionprob=TRUE)

2 个答案:

答案 0 :(得分:1)

如果我理解正确,你会得到一些数据的均值和标准差。您已绘制了此直方图,并希望在直方图上绘制正态分布线。可以使用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()

答案 1 :(得分: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