无法在 Matplotlib 直方图上获取 y 轴来显示概率

问题描述 投票:0回答:4

我的数据(pd 系列)看起来像(每日股票收益,n = 555):

S = perf_manual.returns
S = S[~((S-S.mean()).abs()>3*S.std())]

2014-03-31 20:00:00    0.000000
2014-04-01 20:00:00    0.000000
2014-04-03 20:00:00   -0.001950
2014-04-04 20:00:00   -0.000538
2014-04-07 20:00:00    0.000764
2014-04-08 20:00:00    0.000803
2014-04-09 20:00:00    0.001961
2014-04-10 20:00:00    0.040530
2014-04-11 20:00:00   -0.032319
2014-04-14 20:00:00   -0.008512
2014-04-15 20:00:00   -0.034109
...

我想由此生成概率分布图。使用:

print stats.normaltest(S)

n, bins, patches = plt.hist(S, 100, normed=1, facecolor='blue', alpha=0.75)
print np.sum(n * np.diff(bins))

(mu, sigma) = stats.norm.fit(S)
print mu, sigma
y = mlab.normpdf(bins, mu, sigma)
plt.grid(True)
l = plt.plot(bins, y, 'r', linewidth=2)

plt.xlim(-0.05,0.05)
plt.show()

我得到以下信息:

NormaltestResult(statistic=66.587382579416982, pvalue=3.473230376732532e-15)
1.0
0.000495624926242 0.0118790391467

我的印象是 y 轴是计数,但我想要概率。我该怎么做? 我已经尝试了很多 StackOverflow 答案,但无法解决这个问题。

python matplotlib histogram probability-density
4个回答
10
投票

(据我所知)没有简单的方法可以使用

plt.hist
来做到这一点。但您可以简单地使用
np.histogram
对数据进行分类,然后以您想要的任何方式标准化数据。如果我理解正确的话,您希望数据显示在给定箱中找到点的概率,而不是概率分布。这意味着您必须缩放数据,使所有 bin 的总和为 1。这可以简单地通过执行
bin_probability = n/float(n.sum())
来完成。

您将不再拥有正确归一化的概率分布函数(pdf),这意味着一个区间内的积分将不再是概率!这就是为什么您必须重新调整

mlab.normpdf
以获得与直方图相同的范数的原因。所需的因子只是 bin 宽度,因为当您从正确标准化的 binned pdf 开始时,所有 bin 的总和乘以它们各自的宽度为 1。现在您希望 bin 的总和等于 1。因此缩放因子为bin 宽度。

因此,您最终得到的代码类似于:

import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

# Produce test data
S = np.random.normal(0, 0.01, size=1000)

# Histogram:
# Bin it
n, bin_edges = np.histogram(S, 100)
# Normalize it, so that every bins value gives the probability of that bin
bin_probability = n/float(n.sum())
# Get the mid points of every bin
bin_middles = (bin_edges[1:]+bin_edges[:-1])/2.
# Compute the bin-width
bin_width = bin_edges[1]-bin_edges[0]
# Plot the histogram as a bar plot
plt.bar(bin_middles, bin_probability, width=bin_width)

# Fit to normal distribution
(mu, sigma) = stats.norm.fit(S)
# The pdf should not normed anymore but scaled the same way as the data
y = mlab.normpdf(bin_middles, mu, sigma)*bin_width
l = plt.plot(bin_middles, y, 'r', linewidth=2)

plt.grid(True)
plt.xlim(-0.05,0.05)
plt.show()

最终的图片将是:


6
投票

jotasi 的答案当然有效,但我想添加一个非常简单的技巧,通过直接调用

hist
来实现此目的。

技巧是使用

weights
参数。默认情况下,您传递的每个数据点的权重均为 1。每个 bin 的高度就是落入该 bin 的数据点的权重之和。相反,如果我们有
n
个点,我们可以简单地使每个点的权重为
1 / n
。那么,落入某个桶的点的权重之和就是给定点在该桶中的概率。

在你的情况下,只需将情节线更改为:

n, bins, patches = plt.hist(S, weights=np.ones_like(S) / len(S),
                            facecolor='blue', alpha=0.75)

0
投票

加布里埃尔的回答最初对我不起作用。但原因是我还使用了

density=True
参数。尽管没有在任何地方明确提及,但如果您使用此参数,matplotlib 似乎会忽略您的权重值,并且也不为您提供任何错误。


0
投票

matplotlib plt.hist 文档本身提供了一个更简单的版本。

counts, bins = np.histogram(data)
plt.hist(bins[:-1], bins, weights=counts)
© www.soinside.com 2019 - 2024. All rights reserved.