【问题标题】:Why am I getting multiple lines while plotting distributions? [duplicate]为什么在绘制分布时会出现多条线? [复制]
【发布时间】:2018-09-02 18:44:36
【问题描述】:

我有数据试图拟合正态分布和对数正态分布。 df.head(10)

year    Q
1885     7241
1886     9164
1887     7407
1888     6870
1889     9855
1890    11887
1891     8827
1892     7546
1893     8498
1894    16757
Name: Q, dtype: int64

拟合分布

from scipy import stats
mean = df['Q'].mean()
std = df['Q'].std()
print(mean, std)
6636.172413793103 3130.779541854595

#Fitting
distnormal = stats.norm.pdf(df['Q'], loc = mean, scale = std)
distlognormal = stats.pearson3.pdf(df['Q'], skew = 1, loc = mean, scale = std)

# Plotting
df.hist(bins=10, edgecolor='#4aaaaa', density = True)
plt.plot(df['Q'], distnormal, color = 'red')
plt.plot(df['Q'], distlognormal, color = 'blue')
plt.show()

但是我得到了这样一个情节太多的情节。如何正确拟合分布?

【问题讨论】:

    标签: python matplotlib scipy statistics distribution


    【解决方案1】:

    您将df['Q'] 作为x 参数传递给plt.plot。正如您的数据 sn-p 所示,df['Q'] 中的值未排序 - 这就是问题的原因。在绘制之前尝试按Q 列对数据框进行排序。

    【讨论】:

    • 啊。很简单。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 2021-10-21
    • 2019-04-07
    • 2020-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多