Plotting histogram with dictionary values

Hi I have a list (Key values) with values I would like to plot to a
matplotlib histogram

[('a', 155), ('c', 73), ('b', 19), ('e', 260), ('d', 73), ('g', 42), ('f',
47), ('i', 175), ('h', 77), ('k', 7), ('j', 2), ('m', 76), ('l', 63), ('o',
174), ('n', 145), ('q', 3), ('p', 61), ('s', 153), ('r', 143), ('u', 50),
('t', 193), ('w', 19), ('v', 21), ('y', 55), ('x', 4), ('z', 4)]

Can anyone suggest how do I go about to plot it into a histogram with x-asix
being the key and y axis being the value.?

···

--
View this message in context: http://old.nabble.com/Plotting-histogram-with-dictionary-values-tp30959480p30959480.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

I would do something like the following:

data = [(‘a’, 155), (‘c’, 73), (‘b’, 19), (‘e’, 260), (‘d’, 73), (‘g’, 42), (‘f’,

47), (‘i’, 175), (‘h’, 77), (‘k’, 7), (‘j’, 2), (‘m’, 76), (‘l’, 63), (‘o’,

174), (‘n’, 145), (‘q’, 3), (‘p’, 61), (‘s’, 153), (‘r’, 143), (‘u’, 50),

(‘t’, 193), (‘w’, 19), (‘v’, 21), (‘y’, 55), (‘x’, 4), (‘z’, 4)]

x, y = zip(*data)
xlocs = np.arange(len(x))

fig = plt.figure()
ax = fig.gca()
ax.bar(xlocs + 0.6, y)

ax.set_xticks(xlocs + 1)
ax.set_xticklabels(x)
ax.set_xlim(0.0, xlocs.max() + 2)

plt.show()

The xlocs + 0.6 is to list the left edge of the bars so that they are centered on integer tick locations (the bars are 0.8 wide by default). The xlocs + 1 is to set the xaxis ticks at the center of each bar, and the xlocs.max() + 2 is to have enough room on the right hand side of your graph for everything.

I hope that helps!

Ben Root

···

On Fri, Feb 18, 2011 at 9:13 AM, JamesTan12 <lance103@…83…32…> wrote:

Hi I have a list (Key values) with values I would like to plot to a

matplotlib histogram

[(‘a’, 155), (‘c’, 73), (‘b’, 19), (‘e’, 260), (‘d’, 73), (‘g’, 42), (‘f’,

47), (‘i’, 175), (‘h’, 77), (‘k’, 7), (‘j’, 2), (‘m’, 76), (‘l’, 63), (‘o’,

174), (‘n’, 145), (‘q’, 3), (‘p’, 61), (‘s’, 153), (‘r’, 143), (‘u’, 50),

(‘t’, 193), (‘w’, 19), (‘v’, 21), (‘y’, 55), (‘x’, 4), (‘z’, 4)]

Can anyone suggest how do I go about to plot it into a histogram with x-asix

being the key and y axis being the value.?