1
freq = {1: 1000, 2: 980, 4: 560, ... 40: 3, 41: 1, 43: 1}

(Between 1 to 43, not all numbers are keys)

I want to make a histogram and plot it, such that I can see each key on the X axis, and the values on Y axis using matplotlib. How do I do this? I do not wish to create bins (need all values covered individually) and no tutorial is really helpful to make me understand the terms. I am also short on time, so couldn't get into all the terminology. What is the best way to do this?

pratik_m
  • 67
  • 1
  • 9
  • 1
    If I get you correctly, what you are looking for is not a histogram, just a bar chart. See: http://matplotlib.org/examples/pylab_examples/barchart_demo.html – Tzach Mar 05 '15 at 06:47
  • Are you using any library? like matplotlib? – styvane Mar 05 '15 at 06:50
  • @pratik_m you need 2D histogram Iike in my answer or bar? – styvane Mar 05 '15 at 07:08
  • I needed the bar chart as @Tzach kindly suggested. I shall modify the question title now for anyone else who might benefit from a similar query. For reference, the original question was: "Creating a simple python histogram using data in dictionary" – pratik_m Mar 05 '15 at 12:59

2 Answers2

3

To extend @Tzach's comment, here's a minimal example to create a bar chart from your data:

import matplotlib.pyplot as plt
freq = {1: 1000, 2: 980, 4: 560, 40: 3, 41: 1, 43: 1}
fig, ax = plt.subplots()
ax.bar(freq.keys(), freq.values())
fig.savefig("bar.png")

enter image description here

Carsten
  • 17,991
  • 4
  • 48
  • 53
2

If you use matplotlib you can create 2D histograms

>>> import matplotlib.pyplot as plt
>>> freq = {1: 1000, 2: 980, 4: 560,40: 3, 41: 1, 43: 1}
>>> x = list(freq.keys())
>>> y = list(freq.values())
>>> plt.hist2d(x,y)
(array([[ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  2.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 3.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]]), array([  1. ,   5.2,   9.4,  13.6,  17.8,  22. ,  26.2,  30.4,  34.6,
        38.8,  43. ]), array([    1. ,   100.9,   200.8,   300.7,   400.6,   500.5,   600.4,
         700.3,   800.2,   900.1,  1000. ]), <matplotlib.image.AxesImage object at 0xb475012c>)
>>> plt.show()

histogram 2d

styvane
  • 59,869
  • 19
  • 150
  • 156