Matplotlib 累积直方图 - 垂直线放置错误或误解? [英] Matplotlib cumulative histogram - vertical line placement bug or misinterpretation?

查看:37
本文介绍了Matplotlib 累积直方图 - 垂直线放置错误或误解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否是一个错误,或者我是否只是误解了 matplotlib 的累积直方图的输出.例如,我期望的是在某个x值处,相应的y值会告诉我< = x."有多少个样本.

I am not sure if this is a bug or if I am simply misinterpreting the output of matplotlib's cumulative histogram. E.g., what I expect is "at a certain x value, the corresponding y-value tells me how many samples are <= x."

import matplotlib.pyplot as plt

X = [1.1, 3.1, 2.1, 3.9]
n, bins, patches = plt.hist(X, normed=False, histtype='step', cumulative=True)
plt.ylim([0, 5])
plt.grid()
plt.show()

看到 x=1.9 处的第二条垂直线了吗?给定 X 中的数据,它不应该是2.1吗?例如,在x = 3时,我将读取"3个样本具有x <= 3.1的值"....

See the 2nd vertical line at x=1.9? Shouldn't it be at 2.1 given the data in X? E.g., at x=3 I would read "3 samples have a value x <= 3.1" ...

所以,基本上我期望的是与该步骤图类似的东西.

So, basically what I would expect is something similar to this step plot.

plt.step(sorted(X), range(1, len(X)+1), where='post')
plt.ylim([0, 5])
plt.grid()

我正在使用 python 3.4.3 &matplotlib 1.4.3

I am using python 3.4.3 & matplotlib 1.4.3

推荐答案

如果未设置bins 参数 自己,plt.hist 会为你选择(默认为 10 个)bins:

If you do not set the bins parameter yourself, plt.hist will choose (by default, 10) bins for you:

In [58]: n, bins, patches = plt.hist(X, normed=False, histtype='step', cumulative=True)

In [59]: bins
Out[59]: 
array([ 1.1 ,  1.38,  1.66,  1.94,  2.22,  2.5 ,  2.78,  3.06,  3.34,
        3.62,  3.9 ])

返回值 bins 显示了 matplotlib 选择的 bin 的边缘.

The return value bins shows the edges of the bins that matplotlib chose.

听起来您希望 X 中的值用作 bin 边缘.使用 bins = sorted(X)+ [np.inf] :

It sounds like you want the values in X to serve as bin edges. Using bins=sorted(X)+[np.inf]:

import numpy as np
import matplotlib.pyplot as plt

X = [1.1, 3.1, 2.1, 3.9]
bins = sorted(X) + [np.inf]
n, bins, patches = plt.hist(X, normed=False, histtype='step', cumulative=True, 
                            bins=bins)
plt.ylim([0, 5])
plt.grid()
plt.show()

收益

[np.inf] 使最终 bin 的右边缘扩展到无穷大.Matplotlib 足够聪明,不会尝试绘制非有限值,因此您看到的只是最后一个 bin 的左边缘.

The [np.inf] makes the right edge of the final bin extend to infinity. Matplotlib is smart enough to not try to draw non-finite values, so all you see is the left-edge of the last bin.

这篇关于Matplotlib 累积直方图 - 垂直线放置错误或误解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆