所以我刚从汗学院学到了直方图:

当我在Matplotlib中绘制类似的东西时,它的绘制方式是不同的。为什么?

垃圾箱不应该完全装满吗?既然bin 5-6有3个计数(5,6,6),那么它不应该由一个值3的单条组成吗?我很困惑
发布于 2019-12-13 08:43:42
默认情况下,plt.hist()创建10个桶(或11个边)。默认值是在文件中,并取自您的rc参数rcParams["hist.bins"] = 10。
因此,如果您提供范围为1-6的数据,hist将计算出回收箱中的值数:[1.1-1.5)、[1.5-2.)、[2-2.5.)、[2.5-3.)、[3-3.5.5.)、[3.5-4.)、[4-4.5.5.5)、[5.5-5.5)、5.5-6.通过查看hist()的文本输出(除了图表),就可以看出情况是这样的。
调用hist() 返回3个对象时:
概述:
如果您想拥有宽度为1的条,那么您需要指定回收箱的数量(5),或者指定回收箱的边缘。
这两个呼叫提供了相同的结果:
plt.hist(counts, bins=5)
plt.hist(counts, bins=[1,2,3,4,5,6]) 编辑这里是一个函数,可以帮助您查看hist选择的“桶”
def hist_and_bins(x, ax=None, **kwargs):
ax = ax or plt.gca()
counts, edges, patches = ax.hist(x, **kwargs)
bin_edges = [[a,b] for a,b in zip(edges, edges[1:])]
ticks = np.mean(bin_edges, axis=1)
tick_labels = ['[{}-{})'.format(l,r) for l,r in bin_edges]
tick_labels[-1] = tick_labels[-1][:-1]+']' # last bin is a closed interval
ax.set_xticks(ticks)
ax.set_xticklabels(tick_labels)
return counts, edges, patches, ax.get_xticks()
fig, (ax1, ax2, ax3) = plt.subplots(1,3, figsize=(9,3))
ax1.hist([1,2,3,4,5,6,6])
hist_and_bins([1,2,3,4,5,6,6], ax=ax2)
hist_and_bins([1,2,3,4,5,6,6], ax=ax3, bins=5, ec='w')
fig.autofmt_xdate()

https://stackoverflow.com/questions/59316530
复制相似问题