ToniAz
ToniAz

Reputation: 440

Python display same bin width for different bin sizes

I have this code

bins = [0,1,10,20,30,40,50,75,100]
plt.figure(figsize=(15,15))
plt.hist(df.v1, 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.

Upvotes: 1

Views: 1894

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40667

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

enter image description here

Upvotes: 4

Related Questions