具有相等条宽的matplotlib直方图

具有相等条宽的matplotlib直方图,matplotlib,Matplotlib,我使用直方图来显示分布。如果垃圾箱的间距是均匀的,那么一切都很好。但如果间隔不同,则条形宽度是合适的(如预期)。有没有一种方法可以独立于垃圾箱的大小来设置酒吧的宽度 我不能把你的问题标为重复的问题,但我想这可能是你想要的 我不确定你会如何理解结果,但你可以使用numpy.histogram来计算你的条形图的高度,然后直接根据任意的x比例来绘制 x = np.random.normal(loc=50, scale=200, size=(2000,)) bins = [0,1,10,20,30

我使用直方图来显示分布。如果垃圾箱的间距是均匀的,那么一切都很好。但如果间隔不同,则条形宽度是合适的(如预期)。有没有一种方法可以独立于垃圾箱的大小来设置酒吧的宽度


我不能把你的问题标为重复的问题,但我想这可能是你想要的


我不确定你会如何理解结果,但你可以使用
numpy.histogram
来计算你的条形图的高度,然后直接根据任意的x比例来绘制

x = np.random.normal(loc=50, scale=200, size=(2000,))
bins = [0,1,10,20,30,40,50,75,100]
fig = plt.figure()
ax = fig.add_subplot(211)
ax.hist(x, bins=bins, edgecolor='k')
ax = fig.add_subplot(212)
h,e = np.histogram(x, bins=bins)
ax.bar(range(len(bins)-1),h, width=1, edgecolor='k')


编辑下面是对x-tick标签的调整,以便更容易看到对应关系

my_bins = [10, 20, 30, 40, 50, 120]
my_data = [5, 5, 6, 8, 9, 15, 25, 27, 33, 45, 46, 48, 49, 111, 113]

fig = plt.figure()
ax = fig.add_subplot(211)
ax.hist(my_data, bins=my_bins, edgecolor='k')
ax = fig.add_subplot(212)
h,e = np.histogram(my_data, bins=my_bins)
ax.bar(range(len(my_bins)-1),h, width=1, edgecolor='k')
ax.set_xticks(range(len(my_bins)-1))
ax.set_xticklabels(my_bins[:-1])

这不是真正的直方图。首先计算柱状图,然后绘制一个条形图,条形图的中心距其左侧料仓边缘的料仓宽度的一半。结果看起来像我想要的。但是如何使x轴签名与箱子范围相同(10、20、30、40、50、120而不是1、2、3、4、5、6、7),正如我所说的,条形图是按任意比例绘制的,因此只需重新标记刻度以匹配箱子即可。我已经更新了我的答案。
my_bins = [10, 20, 30, 40, 50, 120]
my_data = [5, 5, 6, 8, 9, 15, 25, 27, 33, 45, 46, 48, 49, 111, 113]

fig = plt.figure()
ax = fig.add_subplot(211)
ax.hist(my_data, bins=my_bins, edgecolor='k')
ax = fig.add_subplot(212)
h,e = np.histogram(my_data, bins=my_bins)
ax.bar(range(len(my_bins)-1),h, width=1, edgecolor='k')
ax.set_xticks(range(len(my_bins)-1))
ax.set_xticklabels(my_bins[:-1])