在matplotlib中将非normpdf拟合到直方图 [英] Fitting non-normpdf's to histograms in matplotlib
问题描述
我正在尝试拟合直方图,但没有任何运气.关于如何在正态概率密度函数中执行此操作的信息很多,但对于其他类型的pdf而言却不是.
I'm trying to do a fit to a histogram but without any luck. There's a bunch of information about how to do it in a normal probability density function but not to other types of pdf's.
import pylab as py
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import exponpow
# Fit procedure
eigfit=[]
for i in range(0,len(eigenvals1)):
if eigenvals1>=4.3:
eigfit.append(eigenvals1[i])
b = exponpow.fit(eigfit)
但我找不到如何绘制此指数 pdf 的曲线.它为我提供了一些合适的值,但是在scipy文档中,并没有很好地说明它是如何实现的.
But I cannot find how to plot the curve for this exponential pdf. It gives me a couple of values from the fit, but in scipy docs it's not well explained how it does this fit.
推荐答案
fit
方法执行参数的最大似然估计.
要拟合 exponpow
分布,您可能希望位置参数(所有 scipy 分布都具有)为 0.还有另外两个参数:尺度参数和形状参数.这是使用 exponpow.fit
的示例(在 ipython 会话中),位置参数固定为 0:
To fit an exponpow
distribution, you probably want the location parameter (that all scipy distributions have) to be 0. There are two other parameters: a scale parameter and a shape parameter. Here's an example (in an ipython session) of using exponpow.fit
, with the location parameter fixed at 0:
首先生成一些假数据:
In [108]: np.random.seed(123)
In [109]: samples = 5 + np.random.randn(1000)
使用 exponpow.fit
将参数拟合到数据中.使用 floc=0
将位置参数固定为 0:
Use exponpow.fit
to fit the parameters to the data. Use floc=0
to fix the location parameter to 0:
In [110]: params = exponpow.fit(samples, floc=0)
使用 matplotlib.pyplot.hist
绘制样品的直方图:
Plot a histogram of the samples, using matplotlib.pyplot.hist
:
In [111]: histresult = hist(samples, density=True, facecolor='cyan', alpha=0.25)
使用方法 exponpow.pdf
绘制拟合分布的PDF:
Plot the PDF of the fitted distribution, using the method exponpow.pdf
:
In [112]: x = np.linspace(0, samples.max() + 1, 100)
In [113]: pdf = exponpow.pdf(x, *params)
In [114]: plot(x, pdf, 'b-', linewidth=2)
Out[114]: [<matplotlib.lines.Line2D at 0x5b59b90>]
这些是拟合参数的值(形状、位置、比例):
These are the values of the fitted parameters (shape, loc, scale):
In [115]: params
Out[115]: (3.5192555959521199, 0, 6.078044809594477)
这篇关于在matplotlib中将非normpdf拟合到直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!