How to create a histogram from a hash map in python?

I have data in hashmap and I want to create a histogram for this data using keys as bins and values ​​as data.

My details:

N = {1: 12, 2: 15, 3: 8, 4: 4, 5: 1}

What I want to build:

  |
15|    X
  |    X 
  |    X
  | X  X
  | X  X
10| X  X
  | X  X
  | X  X  X
  | X  X  X
  | X  X  X
 5| X  X  X
  | X  X  X  X
  | X  X  X  X
  | X  X  X  X
  | X  X  X  X  X
  |_________________________
    1  2  3  4  5

I tried to figure out how to do this with pyplot.hist(), but all the overloads that I can find take a list of values, not a hash map. Do I really need to create this list, just so that matplotlib counts all the values ​​again?

+5
source share
2 answers

Just build a histogram. It histdoes everything .

eg:.

import matplotlib.pyplot as plt

N = {1: 12, 2: 15, 3: 8, 4: 4, 5: 1}
plt.bar(N.keys(), N.values(), align='center')
plt.show()

enter image description here

+15
source

You can easily get the list:

my_list = N.values()

This structure is called a dictionary in Python BTW.

+1
source

All Articles