python中的手动直方图图

piy*_*ush 3 python numpy matplotlib histogram pandas

我正在使用matplotlib制作直方图。

基本上,我想知道是否有任何方法可以手动设置垃圾箱及其值,并仍然获得结果,就好像该图是用matplotlin直方图制作的一样。以下是我的垃圾箱及其相应的值。

0-7     0.9375
7-13    0.9490740741
13-18   0.8285714286 
18-28   0.880952381
28-37   0.92164903
37-48   0.9345357019
48-112  0.9400368773
Run Code Online (Sandbox Code Playgroud)

这是我实施的条形码

import matplotlib.pyplot as plt
plt.bar(maxlist, mean)
plt.show()

maxlist = [7.0, 13.0, 18.0, 28.0, 37.0, 48.0, 112.0]
mean = [0.9375, 0.94907407407777766, 0.82857142858571442, 0.88095238094999995, 0.92164902996666676, 0.93453570191250002, 0.94003687729000018]
Run Code Online (Sandbox Code Playgroud)

这就是上面代码的条形图。 histwithoutwidth

我也尝试过使用width参数,但是在这种情况下,条形似乎重叠了,在直方图中不会发生。

import matplotlib.pyplot as plt
plt.bar(maxlist, mean,width = 6)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我想要的是条形图,看起来像直方图,如下所示,没有任何重叠。有任何想法的人如何在python / ipython笔记本中执行此操作。在此处输入图片说明

小智 5

从@ roadrunner66链接的文档中,

matplotlib.pyplot.bar(left, height, width=0.8, bottom=None, hold=None, data=None, **kwargs)

制作一个以矩形为边界的条形图:

leftleft + widthbottombottom + height

(左,右,下和上边缘)

传递给的第一个参数bar实际上是“ bin”的左边缘。看来maxlist,您所传递left的实际上是垃圾箱的右边缘,这使所有东西都丢掉了,并导致宽度变得怪异。

import matplotlib.pyplot as plt
import numpy as np
x1 = [0, 7, 13, 18, 28, 37, 48] #left edge
x2 = x1[1::] + [112] #right edge
y = [0.9375, 0.94907407407777766, 0.82857142858571442, 0.88095238094999995, 0.92164902996666676, 0.93453570191250002, 0.94003687729000018]
w = np.array(x2) - np.array(x1) #variable width, can set this as a scalar also
plt.bar(x1, y, width=w, align='edge')
plt.show()
Run Code Online (Sandbox Code Playgroud)

条带宽度可变的条形图

条形宽度代表左右边缘之间的距离,x1x2。如果您想要恒定的宽度,只需将传递width=w_scalarplt.bar(),这w_scalar是您恒定的纸槽宽度。如果您希望条形物之间的距离更远,则将其width缩小。