【问题标题】:display a histogram with very non-uniform bin widths显示具有非常不均匀的 bin 宽度的直方图
【发布时间】:2016-02-03 12:19:19
【问题描述】:

这是直方图

为了生成这个图,我做了:

bins = np.array([0.03, 0.3, 2, 100])
plt.hist(m, bins = bins, weights=np.zeros_like(m) + 1. / m.size)

但是,正如您所注意到的,我想绘制每个数据点的相对频率的直方图,其中只有 3 个大小不同的 bin:

bin1 = 0.03 -> 0.3

bin2 = 0.3 -> 2

bin3 = 2 -> 100

直方图看起来很难看,因为最后一个 bin 的大小相对于其他 bin 非常大。如何修复直方图?我想改变箱子的宽度,但我不想改变每个箱子的范围。

【问题讨论】:

  • 但它不再是直方图了,是吗?
  • @cel,不,可以是bar graph
  • 好吧,你试过绘制条形图吗?您可以从 np.histogram 获得每个 bin 中的计数数,因此实现应该是直截了当的。
  • @cel - 是的,我试过了。我仍然没有找到改变 xaxis 数字的方法。

标签: python matplotlib histogram


【解决方案1】:

正如@cel 所指出的,这不再是直方图,但您可以使用plt.barnp.histogram 执行您所要求的操作。然后,您只需将 xticklabels 设置为描述 bin 边缘的字符串。例如:

import numpy as np
import matplotlib.pyplot as plt

bins = [0.03,0.3,2,100] # your bins
data = [0.04,0.07,0.1,0.2,0.2,0.8,1,1.5,4,5,7,8,43,45,54,56,99] # random data

hist, bin_edges = np.histogram(data,bins) # make the histogram

fig,ax = plt.subplots()

# Plot the histogram heights against integers on the x axis
ax.bar(range(len(hist)),hist,width=1) 

# Set the ticks to the middle of the bars
ax.set_xticks([0.5+i for i,j in enumerate(hist)])

# Set the xticklabels to a string that tells us what the bin edges were
ax.set_xticklabels(['{} - {}'.format(bins[i],bins[i+1]) for i,j in enumerate(hist)])

plt.show()

编辑

如果你更新到 matplotlib v1.5.0,你会发现 bar 现在需要一个 kwarg tick_label,这可以使这个绘图更加容易(see here):

hist, bin_edges = np.histogram(data,bins)

ax.bar(range(len(hist)),hist,width=1,align='center',tick_label=
        ['{} - {}'.format(bins[i],bins[i+1]) for i,j in enumerate(hist)])

【讨论】:

  • 当我这样做时,我得到两个图而不是一个,并且文本没有居中编辑:更新版本效果更好,我仍然得到两个图
  • 亲爱的,不再需要添加 0.5 来设置 xticks。也许是由于 matplotlib 的变化。
  • @saurabheights 是的,您会在我上面的答案的编辑中注意到这已经说明了
【解决方案2】:

如果您的 bin 实际值并不重要,但您希望获得完全不同数量级的值的直方图,则可以使用沿 x 轴的对数缩放。这里为您提供等宽的条形

import numpy as np
import matplotlib.pyplot as plt

data = [0.04,0.07,0.1,0.2,0.2,0.8,1,1.5,4,5,7,8,43,45,54,56,99]

plt.hist(data,bins=10**np.linspace(-2,2,5)) 
plt.xscale('log')

plt.show()

当你必须使用你的 bin 值时,你可以这样做

import numpy as np
import matplotlib.pyplot as plt

data = [0.04,0.07,0.1,0.2,0.2,0.8,1,1.5,4,5,7,8,43,45,54,56,99]
bins = [0.03,0.3,2,100] 

plt.hist(data,bins=bins) 
plt.xscale('log')

plt.show()

但是,在这种情况下,宽度并不完全相等,但仍然可读。如果宽度必须相等并且您必须使用您的垃圾箱,我推荐@tom 的解决方案。

【讨论】: