Wednesday, February 3, 2010

Multiple bars in a bar-graph in matplotlib


I answered a question on Stack Overflow. It was posted 3 hr ago, and the guy's never coming back, so I'll put it up here and you can enjoy it. If you want the axes all purty and everything, check out the manual.


import numpy as np
from pylab import *

data = [[6.7, 1.5, 4.5], [2.0, 3.25, 5.7]]
w = 0.5
xlocations = np.array(range(len(data)))+w
colors = ['0.2','0.5','0.8']

oL = list()
for x,d in zip(xlocations, data):
for c,value in zip(colors, sorted(d,reverse=True)):
b = bar(x, value, width=w, color=c)
oL.extend(b)
show()