Python Seaborn距离图:具有固定参数的拟合分布
我想对给定的数据数组Python Seaborn距离图:具有固定参数的拟合分布,python,matplotlib,seaborn,data-fitting,Python,Matplotlib,Seaborn,Data Fitting,我想对给定的数据数组x拟合一些分布,比如gamma,并绘制相应的密度函数。我可以通过seaborn.distplot和scipy.stats轻松做到这一点: sns.distplot(x, fit = stats.gamma) 但是,假设我希望此分布的一些参数保持不变,例如loc。当我使用scipy.stats中的fit函数和fixedloc时,我将其写成 stats.gamma.fit(x, floc = 0) 有没有办法将distplot函数中的loc=0传递到fit并获得相同的结果?条
x
拟合一些分布,比如gamma,并绘制相应的密度函数。我可以通过seaborn.distplot
和scipy.stats
轻松做到这一点:
sns.distplot(x, fit = stats.gamma)
但是,假设我希望此分布的一些参数保持不变,例如loc
。当我使用scipy.stats
中的fit
函数和fixedloc
时,我将其写成
stats.gamma.fit(x, floc = 0)
有没有办法将
distplot
函数中的loc=0
传递到fit
并获得相同的结果?条件是sns.distplot(x,fit=stats.gamma)
确实会显示一个合理的绘图,并且stats.gamma.fit(x,loc=0)
会给出所需的统计数据,您可以通过fit_kws
提供参数:
sns.distplot(x, fit = stats.gamma, fit_kws={"loc" : 0})
[此答案基于阅读,未经测试,因为问题中未给出任何用例。]最简单的方法是不使用distplot绘制拟合,而是使用所述方法。提供了一个简单的例子:
import matplotlib.pyplot as plt
import pandas as pd
from scipy import stats
import numpy as np
df = pd.DataFrame(np.random.gamma(2, scale=2, size=5000),
columns=['samples'])
params = stats.gamma.fit(df.samples, loc=0)
xvals = np.linspace(0, df.samples.max())
pdf = lambda x: stats.gamma.pdf(xvals, *params)
yvals = pdf(xvals)
fig, ax1 = plt.subplots()
df.samples.hist(bins=20, ax=ax1, normed=True, label='Samples',
grid=False, edgecolor='k')
plt.plot(xvals, yvals, axes=ax1, c='r', label='Fit')
ax1.legend()
这将导致类似于。。。
没有。属性错误:未知财产弗洛卡斯回答说,“在条件下……”。现在看来这个问题有错误。它应该是
loc
而不是floc
。