在直方图上方绘制密度函数 [英] Plot a density function above a histogram
问题描述
在Python中,我已经估算了分布模型密度的参数,我想在分布的直方图上方绘制密度函数.在 R 中它类似于使用选项 prop=TRUE
.
将 numpy 导入为 np将matplotlib.mlab导入为mlab导入matplotlib.pyplot作为plt# 列表数据"的初始化# 估计参数,在我的例子中,是正态分布的均值和方差plt.hist(data,bins ="auto")#data是数据列表# 这里我想在直方图上方绘制密度plt.show()
我想最棘手的部分是使其变得合适.
我根据第一个答案尝试了这个:
mean = np.mean(logdata)var = np.var(logdata)std = np.sqrt(var) # 标准差,被 numpy 用作方差的替代plt.hist(logdata,bins ="auto",alpha = 0.5,label =donnéesempiriques")x = np.linspace(min(logdata), max(logdata), 100)plt.plot(x,mlab.normpdf(x,平均值,标准))plt.xlabel("log(tailed des fichiers)")plt.ylabel("nombre de fichiers")plt.legend(loc ='右上')plt.grid(真)plt.show()
但它不适合图表,这是它的外观:
** Edit 2 ** 与直方图函数中的选项 normed=True
一起使用.
如果我理解正确,你就会得到一些数据的均值和标准差.您已经绘制了一个直方图,并希望在直方图上绘制正态分布线.可以使用 matplotlib.mlab.normpdf()
生成此行,可以在
以上仅适用于 normed = True
.如果这不是一个选项,我们可以定义我们自己的函数:
def gauss_function(x,a,x0,sigma):返回一个 * np.exp(-(x - x0) ** 2/(2 * sigma ** 2))平均值 = 100西格玛= 5data = np.random.normal(mean,sigma,1000) # 生成假数据x = np.linspace(最小(数据),最大(数据),1000)测试= gauss_function(x,max(data),平均值,sigma)plt.hist(data,bins ="auto")plt.plot(x, 测试)plt.show()
In Python, I have estimated the parameters for the density of a model of my distribution and I would like to plot the density function above the histogram of the distribution. In R it is similar to using the option 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()
I guess the trickiest part is to make it fit.
Edit: I have tried this according to the first answer:
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()
But it doesn't fit the graph, here is how it looks:
** Edit 2 ** Works with the option normed=True
in the histogram function.
If I understand you correctly you have the mean and standard deviation of some data. You have plotted a histogram of this and would like to plot the normal distribution line over the histogram. This line can be generated using matplotlib.mlab.normpdf()
, the documentation can be found 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()
Which gives the following figure:
Edit: The above only works with normed = True
. If this is not an option, we can define our own function:
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()
这篇关于在直方图上方绘制密度函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!