noob
noob

Reputation: 3811

Histogram not specifying desired bins in pandas

Code:

 np.histogram(df['columnforhistogram'], bins=(np.arange(start=0, stop=2000, step=25)), density=True)
 plt.xlabel("Column")
 plt.ylabel('Bins')
 plt.show()

Output I want:

 I want a histogram with bins starting from 0 , ending at 2000 and at an 
 interval of 25.'
 X-axis should have values 0,25,50 and so on...

Output I am getting

 A blank histogram with values in x-axis from 0 to 1 with interval of 0.2 
 (0,0.2,0.4 ..... 1 on both x - axis and y - axis)

Upvotes: 0

Views: 1295

Answers (1)

As far as I can tell np.histogram does not plot a histogram. It simply returns an array of frequencies and an array of edges (check the numpy.histogram documentation ). That's probably why you're getting a blank plot.

You should plt.hist() in order to plot those values before calling plt.show().

hist, bins = np.histogram(df['columnforhistogram'], bins=(np.arange(start=0, stop=2000, step=25)), density=True)
my_hist = plt.hist(hist, bins=bins)
plt.xlabel("Column")
plt.ylabel('Bins')
plt.show()

Alternatively, you could jump straight into pyplot:

_ = plt.plot(df['columnforhistogram'], bins=np.arange(0,2000,25),density=True)
plt.xlabel('Column')
plt.ylabel('Bins')
plt.show()

Upvotes: 1

Related Questions