I created a dictionary that takes into account the entries in the list each key, and now I would like to build a histogram of its contents.
This is the content of the dictionary I want to build:
{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 have written 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 just to do
plt.bar(myDictionary.keys(), myDictionary, width, color='g')
but this is the result:

and I donβt know why 3 bars are shifted, and also I would like the histogram to be displayed in an orderly way.
Can someone tell me how to do this?
python dictionary matplotlib histogram
Matteo
source share