Python 从字典中绘制直方图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21195179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:15:44  来源:igfitidea点击:

Plot a histogram from a Dictionary

pythondictionarymatplotlibhistogram

提问by Matteo

I created a dictionarythat counts the occurrences in a listof every key and I would now like to plot the histogram of its content.

我创建了一个dictionary计算list每个键a中出现的次数,现在我想绘制其内容的直方图。

This is the content of the dictionary I want to plot:

这是我要绘制的字典的内容:

{1: 27, 34: 1, 3: 72, 4: 62, 5: 33, 6: 36, 7: 20, 8: 12, 9: 9, 10: 6, 11: 5, 12: 8, 2: 74, 14: 4, 15: 3, 16: 1, 17: 1, 18: 1, 19: 1, 21: 1, 27: 2}

So far I wrote this:

到目前为止,我写了这个:

import numpy as np
import matplotlib.pyplot as plt

pos = np.arange(len(myDictionary.keys()))
width = 1.0     # gives histogram aspect to the bar diagram

ax = plt.axes()
ax.set_xticks(pos + (width / 2))
ax.set_xticklabels(myDictionary.keys())

plt.bar(myDictionary.keys(), ******, width, color='g')
#                            ^^^^^^ what should I put here?
plt.show()

I tried by simply doing

我试着简单地做

plt.bar(myDictionary.keys(), myDictionary, width, color='g')

but this is the result:

但这是结果:

enter image description here

在此处输入图片说明

and I don't know why the 3 bars are shifted and also I'd like the histogram to be displayed in a ordered fashion.

我不知道为什么 3 个条形图会移动,而且我希望直方图以有序的方式显示。

Can somebody tell me how to do it?

有人能告诉我怎么做吗?

采纳答案by Alvaro Fuentes

You can use the function for plotting histograms like this:

您可以使用该函数绘制直方图,如下所示:

a = np.random.random_integers(0,10,20) #example list of values
plt.hist(a)
plt.show()

Or you can use myDictionaryjust like this:

或者你可以myDictionary像这样使用:

plt.bar(myDictionary.keys(), myDictionary.values(), width, color='g')

回答by mcsilvio

values = [] #in same order as traversing keys
keys = [] #also needed to preserve order
for key in myDictionary.keys():
  keys.append(key)
  values.append(myDictionary[key])

Use 'keys' and 'values'. This ensures that the order is preserved.

使用“键”和“值”。这可确保保留订单。

回答by Franck Dernoncourt

With Python 3 you need to use list(your_dict.keys())instead of your_dict.keys()(otherwise you get TypeError: 'dict_keys' object does not support indexing):

使用 Python 3 你需要使用list(your_dict.keys())而不是your_dict.keys()(否则你会得到TypeError: 'dict_keys' object does not support indexing):

import matplotlib.pyplot as plt

dictionary = {1: 27, 34: 1, 3: 72, 4: 62, 5: 33, 6: 36, 7: 20, 8: 12, 9: 9, 10: 6, 11: 5, 
              12: 8, 2: 74, 14: 4, 15: 3, 16: 1, 17: 1, 18: 1, 19: 1, 21: 1, 27: 2}
plt.bar(list(dictionary.keys()), dictionary.values(), color='g')
plt.show()

enter image description here

在此处输入图片说明

Tested with Matplotlib 2.0.0 and python 3.5.

使用 Matplotlib 2.0.0 和 python 3.5 进行测试。

FYI: Plotting a python dict in order of key values

仅供参考:按键值的顺序绘制python dict

回答by organicData

In case the keys of the myDictionaryare not uniformed distributed, its helpful to use the keys as str:

如果密钥myDictionary不是统一分布的,将密钥用作str

plt.bar([ str(i) for i in myDictionary.keys()], myDictionary.values(), color='g')