试图了解scipy.stats.norm

时间:2019-06-25 23:55:48

标签: matplotlib scipy curve-fitting binning

我正在使用scipy.stats.norm生成具有正态分布重叠的直方图。

此MWE

import pandas as pd
import matplotlib.pyplot as plt
import scipy.stats as stats
import numpy as np

length = 1000
np.random.seed(100)

dictOne = {
           "A": np.random.randn(length),
           "B": np.random.randn(length),
           "C": np.random.randn(length)}

df2 = pd.DataFrame(dictOne)

column = 'B'
df = df2[df2[column] > -999]

fig, ax = plt.subplots()
h, edges, _ = ax.hist(df[column], alpha = 0.5, density = True, bins = 50)
param = stats.norm.fit(df2[column].dropna())   # Fit a normal distribution to the data
x = np.linspace(*df2[column].agg([min, max]), 1000) # x-values

#pdf_fitted = (x, stats.norm.pdf(df2[column], *param))
binwidth = np.diff(edges).mean()
ax.plot(x, stats.norm.pdf(x, *param)*h.sum()*binwidth, color = 'r')

产生 enter image description here 但是当我应用基本相同的代码时:

fig, ax = plt.subplots()
h, edges, _ = ax.hist(df['delta z'], alpha = 0.5, density = True, bins = 50)

param = stats.norm.fit(df['delta z'].dropna())   # Fit a normal distribution to the data
#pdf_fitted = stats.norm.pdf(df['delta z'], *param)
x = np.linspace(*df['delta z'].agg([min, max]), 1000) # x-values
binwidth = np.diff(edges).mean()
ax.plot(x, stats.norm.pdf(x, *param)*h.sum()*binwidth, color = 'r')

plt.grid(which = 'both')
plt.title(r'$\Delta z$ distribution for %s against %s'%(graph_title, xname), fontsize = 25)
plt.xlabel(r'$\Delta z = z_{spec} - z_{photo}$', fontsize = 25)
plt.ylabel('Number', fontsize = 25)
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
xmin, xmax = min(df['delta z']), max(df['delta z'])
plt.xlim(xmin, xmax)
plt.annotate(
r'''$\mu_{\Delta z}$ = %.3f
$\sigma_{\Delta z}$ = %.3f'''%(param[0], param[1]),
         fontsize = 25, color = 'r', xy=(0.85, 0.85), xycoords='axes fraction')

对于我的实际数据(在一个大文本文件中),我得到了这样的拟合值:

enter image description here

数据在一个大文本文件中,所以我不能在这里包括它。但是我的问题是,玩具示例中的拟合度非常适合数字,但是根据实际数据,我希望红色曲线更接近直方图。有什么我可以做的事情来提高适应性,也许是垃圾箱尺寸之类的?我对scipy.stats知之甚少,无法知道如何使其更合适。

0 个答案:

没有答案