Sunday, March 9, 2014

python: create bar chart

#!/usr/bin/env python
# a bar plot with errorbars
import numpy as np
import matplotlib.pyplot as plt

data = {u'kh': 76, u'us': 1, u'uk': 38, u'cn': 2, u'au': 85, u'ru': 31}
datak = [x for x in data]

datav = [data[v] for v in data]
datav = tuple(datav)

N = len(datak)
ind = np.arange(N)
width = 0.35
print datav
fig, ax = plt.subplots()
rects1 = ax.bar(ind, datav, width, color='r')

def autolabel(rects,lb):
    # attach some text labels
    for rect, xx in zip(rects,lb):
        height = rect.get_height()
        #ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%s'%(xx + ":" + str(height)),
                ha='center', va='bottom')

autolabel(rects1,datak)
plt.show()





No comments:

Post a Comment