本文介绍了Python 为不同的 bin 大小显示相同的 bin 宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码

bins = [0,1,10,20,30,40,50,75,100]
plt.figure(figsize=(15,15))
plt.hist(df.v1, bins = bins)

我的问题是,图中的垃圾箱宽度与它们在 bins 中的范围成正比.但是,我希望所有垃圾箱都具有相同的宽度.

My problem is that the bin widths as they appear in the figure are proportional to their range in bins. However, I want all bins to come out having the same width.

推荐答案

我不确定您如何看待结果,但可以使用 numpy.histogram 计算高度条形,然后直接针对任意x比例绘制这些条形.

I'm not sure how you'll make sense of the result, but you can use numpy.histogram to calculate the height of your bars, then plot those directly against an arbitrary x-scale.

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')

这篇关于Python 为不同的 bin 大小显示相同的 bin 宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 23:01