`np.histogram` 和 `plt.hist` 有什么区别?为什么这些命令不绘制相同的图形? [英] What is the difference between `np.histogram` and `plt.hist`? Why don't these commands plot the same graphics?

查看:40
本文介绍了`np.histogram` 和 `plt.hist` 有什么区别?为什么这些命令不绘制相同的图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:很抱歉,由于正确的注释,代码已更新.图形仍然存在一些问题 - 一个历史记录被转移到另一个.

UPDATE: Sorry again, the code was updated due to correct comments. And there is still some problem with graphics - one hist is shifted to another.

更新:对不起,这些历史记录有不同数量的 bin.而且即使在这一点上,将 plt.hist 中的箱数设置为"5"也无济于事

UPDATE: I'm sorry, these hists have different number of bins. And even at this point setting '5' as number of bins in plt.hist doesn't help

下面的代码在同一个数据源上计算两个直方图.绘制这些直方图表明它们并不重合. np.hist 的标记:它返回两个数组的元组-bin的值,包括边缘bin和许多计数.因此,我认为将bin边缘位置的值居中是合理的.

The code below computes two histograms on the same datasource. And plotting these histograms shows that they don't coincide. A mark for np.hist : it returns a tuple of two arrays - values of bins including edge bins and a number of counts. So I thought that it could be reasonable to center values of bin edge locations.

import numpy as np
import matplotlib.pyplot as plt
s = [1,1,1,1,2,2,2,3,3,4,5,5,5,6,7,7,7,7,7,7,7]

xmin = 1
xmax = 7
step = 1.
print 'nbins=',(xmax-xmin)/step
print np.linspace(xmin, xmax, (xmax-xmin)/step)
h1 = np.histogram(s, bins=np.linspace(xmin, xmax, (xmax-xmin)/step))
print h1
def calc_centers_of_bins(x):
    return  list(x[i]+(x[i]-x[i+1])/2.0 for i in xrange(len(x)-1))

x = h1[1].tolist()
print x
y = h1[0].tolist()


plt.bar(calc_centers_of_bins(x),y, width=(x[-1]-x[0])/(len(y)), color='red', alpha=0.5)
plt.hist(s, bins=5,alpha=0.5)
plt.grid(True)
plt.show()

推荐答案

您在这两种情况下使用了不同的 bin.在您的情况下, np.linspace(xmin,xmax,(xmax-xmin)/step)有5个垃圾箱,但是您已经告诉 plt.hist 使用6个垃圾箱.

You're using different bins in the two cases. In your case, np.linspace(xmin, xmax, (xmax-xmin)/step) has 5 bins, but you've told plt.hist to use 6 bins.

您可以通过查看每个的输出来看到这一点:

You can see this by looking at the output of each:

h1 = np.histogram(s, bins=np.linspace(xmin, xmax, (xmax-xmin)/step))
h_plt = plt.hist(s, bins=6,alpha=0.5)

然后:

>>> h1[1]
array([ 1. ,  2.2,  3.4,  4.6,  5.8,  7. ])
>>> h_plt[1]
array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.])

我会使用:

y, x = np.histogram(s, bins=np.linspace(xmin, xmax, (xmax-xmin)/step))
nbins = y.size
# ...
plt.hist(s, bins=nbins, alpha=0.5)

然后你的直方图匹配,但你的情节仍然不会,因为你已经在 bin 的中心绘制了 np.histogram 的输出,但是 plt.bar 需要一个左边缘数组:

Then your histograms match, but your plot still won't because you've plotted the output of your np.histogram at the centers of the bins, but plt.bar expects an array of the left edges:

plt.bar(left, height, width=0.8, bottom=None,hold=None, **kwargs)

参数
----------
left : 标量序列
条形图左侧的 x 坐标

Parameters
----------
left : sequence of scalars
the x coordinates of the left sides of the bars

height : 标量序列
条形的高度

height : sequence of scalars
the heights of the bars

你想要的是:

import numpy as np
import matplotlib.pyplot as plt
s = [1,1,1,1,2,2,2,3,3,4,5,5,5,6,7,7,7,7,7,7,7]

xmin = 1
xmax = 7
step = 1
y, x = np.histogram(s, bins=np.linspace(xmin, xmax, (xmax-xmin)/step))

nbins = y.size

plt.bar(x[:-1], y, width=x[1]-x[0], color='red', alpha=0.5)
plt.hist(s, bins=nbins, alpha=0.5)
plt.grid(True)
plt.show()

这篇关于`np.histogram` 和 `plt.hist` 有什么区别?为什么这些命令不绘制相同的图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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