views:

41

answers:

2

In matplotlib, line plots color cycle automatically. These two line plots would have different colors.

axes.plot(x1, y)
axes.plot(x2, y)

However, bar plots don't. Both these data series will have blue bars.

axes.bar(x1, y)
axes.bar(x2, y)

How do I make bar plots cycle automatically across a predefined set of colors?

+1  A: 

From the documentation at http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.bar

The bar plots do not cycle color automatically but you could set the color directly by passing the color properties. Something like this:

colors = ['red', 'blue', 'green']
i = -1
def getCycledColor():
    global i, colors
    if i < len(colors) -1
        i = i + 1
        return colors[i]
    else:
        i = -1
axes.bar(x1,y,facecolor=getCycledColor())
axes.bar(x2,y,facecolor=getCycledColor())

The colors can be chosen from a predefined list and cycled.

pyfunc
+1  A: 

Would something along these lines do it for you?

#!/usr/bin/python 
from matplotlib import cm
import matplotlib.pyplot as plt

#data
x=[1,2,4]
y=[11,12,8]

for i in range(0,len(x)):
  plt.bar(x[i],y[i],color=cm.jet(1.*i/len(x)))

plt.show()

More on colormaps.

EDIT: See this example for how to cycle over a predefined set of colors.

Zhenya