Zach Moshe
Zach Moshe

Reputation: 2980

hist() - how to force equal bins width?

Assuming I have the following array: [1,1,1,2,2,40,60,70,75,80,85,87,95] and I want to create a histogram out of it based on the following bins - x<=2, [3<=x<=80], [x>=81].

If I do the following: arr.hist(bins=(0,2,80,100)) I get the bins to be at different widths (based on their x range). I want them to represent different size ranges but appear in the histogram at the same width. Is it possible in an elegant way?

I can think of adding a new column for this (holding the bin id that will be calculated based on the boundaries I want) but don't really like this solution..

Thanks!

Upvotes: 1

Views: 1860

Answers (1)

Lee
Lee

Reputation: 31090

Sounds like you want a bar graph; You could use bar:

import numpy as np
import matplotlib.pyplot as plt
arr=np.array([1,1,1,2,2,40,60,70,75,80,85,87,95])
h=np.histogram(arr,bins=(0,2,80,100))
plt.bar(range(3),h[0],width=1)
xlab=['x<=2', '3<=x<=80]', 'x>=81']
plt.xticks(arange(0.5,3.5,1),xlab)

Upvotes: 2

Related Questions