Shan-Desai
Shan-Desai

Reputation: 3349

dictionary values as column labels in matplotlib

I have the following dict:

packetsNeeded = { u'hydrogen': 150,
                  u'helium': 143,
                  u'lithium': 122}

I am using matplotlib to plot a bar graph for each of the keys and the values of the dictionary above. I have managed to plot the bar chart using the following code:

import matplotlib.pyplot as pplot

pplot.bar(range(len(packetsNeeded)), packetsNeeded.values(),
         width = 0.75, align = 'center')
pplot.xticks(range(len(packetsNeeded)), packetsNeeded.keys())

pplot.xlabel("X label")
pplot.ylabel("Y label")
pplot.title("title here")
pplot.show()

I can show the X-Axis as the keys mentioned in the dictionary but I want the vertical bar chart to show the values of the dictionary on each bar.

I tried

pplot.yticks(range(len(packetsNeeded), packets.needed.values())

but the plot is as follows: yticks messes the y axis up!!

What is the right way to show the labels on columns for a dict

I tried the following:

for a, b in zip( key(), value()):
    pplot.text(a, b, str(b))

but I run into problems

Upvotes: 0

Views: 2061

Answers (1)

Aguy
Aguy

Reputation: 8059

Maybe you've meant something along the lines of

for key, x in zip(packetsNeeded.keys(), range(len(packetsNeeded))): 
    pplot.text(x, packetsNeeded[key], packetsNeeded[key])

Upvotes: 1

Related Questions