【问题标题】:Histogram not specifying desired bins in pandas直方图未在 pandas 中指定所需的 bin
【发布时间】:2019-12-27 10:42:44
【问题描述】:

代码:

 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)

【问题讨论】:

标签: python pandas histogram unordered bins


【解决方案1】:

据我所知,np.histogram 并没有绘制直方图。它只是返回一个频率数组和一个边数组(检查 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()

【讨论】:

  • 您好,我得到了正确的 x 轴值,但我想要 y 轴的变量值,该怎么做
  • 我在 y 轴上得到 2.5、5.0、7.5 等值。我想要 y 轴上的变量值
  • 使用此代码 import matplotlib.pyplot as plt plt.figure(figsize=[10,8]) plt.hist(df['columnforhistogram'], 30, range=[0, 2000], facecolor='gray', align='mid') 我得到一个不同的直方图,在 250 之后的 bin 中也有值,但是在上面的代码中,较高的 bin 没有显示任何值,为什么会这样?有什么想法吗?
猜你喜欢
  • 2022-08-12
  • 1970-01-01
  • 1970-01-01
  • 2021-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多