字典中的Python直方图

时间:2015-05-10 13:33:01

标签: python string dictionary histogram

我有一本看起来像这样的词典:

dict = {1092267: '0.187', 524292: '-0.350', 524293: '0.029', 524294: '0.216'}

所以有一个id,然后是一个字符串内的值(真正的字典包含10 000个这样的id)。我想在x轴上制作直方图,这些字符串中的值的绝对值从0-0.1,0.1-0.2,0.2-0.3等一直到0.9-1.0。 y轴应计算0-0.1等范围内值的出现量。 我该怎么做???

2 个答案:

答案 0 :(得分:3)

您可以使用 python manage.py migrate 创建直方图的分档。 首先,获取所有字典值的绝对值(因为ids是无关紧要的)。

numpy.histogram

然后,使用 dict_values = [abs(float(i)) for i in dict.values()] 明确指定值的范围。

numpy.histogram

答案 1 :(得分:0)

不使用numpy.histogram,您可以尝试:

test = {1092267: '0.187', 524292: '-0.350', 524293: '0.029', 524294: '0.216'}

intervals = [(-2, 0.1), (0.1, 0.2), (0.2, 0.3), (0.3, 0.4)]
count = []

for inf, sup in intervals:
    count.append(len([x for x in test.values() if inf < float(x) < sup]))

然后,count,即直方图,将具有[2,1,1,0]。这样,您可以指定任意范围来定义分档。如果要绘制它,可以使用matplotlib(example here)。

1:http://matplotlib.org/1.3.0/examples/pylab_examples/histogram_demo_extended.html