繁体   English   中英

python中的手动直方图

[英]Manual Histogram plot in python

我正在使用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

这是我实施的条形码

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]

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

我也试过玩宽度参数,但在这种情况下条形似乎重叠,这在直方图中不会发生。

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

在此输入图像描述

我想要的是条形图看起来像直方图,如下所示,没有任何重叠。 有任何想法的人如何在python / ipython笔记本中这样做。 在此输入图像描述

从@ roadrunner66链接的文档中,

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

制作一个带有矩形的条形图:

leftleft + widthbottombottom + height

(左,右,下,上边缘)

传递给bar的第一个参数实际上是“bin”的左边缘。 看起来maxlist ,你left传递的实际上就是你的垃圾箱的右边缘,它maxlist一切都扔掉,导致你的宽度变得很奇怪。

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()

带有可变箱宽的条形图

条宽度代表左右边缘x1x2之间的距离。 如果你想要恒定的宽度,只需将width=w_scalar传递给plt.bar() ,其中w_scalar是你的常量bin宽度。 如果您希望条形物更远,只需将width得更小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM