直方图未在 pandas 中指定所需的 bin

Histogram not specifying desired bins in pandas

代码:

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

我想要的输出:

 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...

我得到的输出

 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)

据我所知 np.histogram 没有 绘制 直方图。它只是 returns 一个频率数组和一个边缘数组(检查 numpy.histogram documentation )。这可能就是您得到空白图的原因。

您应该 plt.hist() 以便在调用 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()

或者,您可以直接跳转到 pyplot:

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