How do I make bar plots automatically cycle across different colors?
Asked Answered
F

3

16

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?

Feudality answered 29/8, 2010 at 4:58 Comment(0)
H
4

Optional: To get full control over the style of your figures use an existing mplstyle as a template: https://github.com/matplotlib/matplotlib/tree/master/lib/matplotlib/mpl-data/stylelib

adjust the parameter : axes.prop_cycle: cycler('color', [....])

load your style:

from matplotlib import style
style.use ('PATH TO YOUR MPL STYLE')

You can cycle through your or the default style color cycle almost any way you want:

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

#data
x=[1,2,4]
y=[11,12,8]
prop_iter = iter(plt.rcParams['axes.prop_cycle'])

for i in range(0,len(x)):
  plt.bar(x[i],y[i],color=next(prop_iter)['color'])

plt.show()

plt.rcParams['axes.prop_cycle'] grabs all cycles so you need to select the correct cycler using the key ['color'].

You can drop the iterator creation and use list comprehension and zip to create one liners:

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

x=[1,2,4]
y=[11,12,8]
prop = plt.rcParams['axes.prop_cycle']
[plt.bar(param[0],param[1],color=param[2]['color']) for param in zip(x,y,prop)]
plt.show()

enter image description here

Hanan answered 13/6, 2016 at 0:32 Comment(0)
R
24

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.

Rowell answered 29/8, 2010 at 5:57 Comment(0)
H
4

Optional: To get full control over the style of your figures use an existing mplstyle as a template: https://github.com/matplotlib/matplotlib/tree/master/lib/matplotlib/mpl-data/stylelib

adjust the parameter : axes.prop_cycle: cycler('color', [....])

load your style:

from matplotlib import style
style.use ('PATH TO YOUR MPL STYLE')

You can cycle through your or the default style color cycle almost any way you want:

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

#data
x=[1,2,4]
y=[11,12,8]
prop_iter = iter(plt.rcParams['axes.prop_cycle'])

for i in range(0,len(x)):
  plt.bar(x[i],y[i],color=next(prop_iter)['color'])

plt.show()

plt.rcParams['axes.prop_cycle'] grabs all cycles so you need to select the correct cycler using the key ['color'].

You can drop the iterator creation and use list comprehension and zip to create one liners:

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

x=[1,2,4]
y=[11,12,8]
prop = plt.rcParams['axes.prop_cycle']
[plt.bar(param[0],param[1],color=param[2]['color']) for param in zip(x,y,prop)]
plt.show()

enter image description here

Hanan answered 13/6, 2016 at 0:32 Comment(0)
P
3

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.

Psychopath answered 29/8, 2010 at 5:46 Comment(1)
that just produces two bar plots, one with all bars red and one with all bars blue (because getCycledColor() is called only once when axes().bar(..) is called)Jaquelinejaquelyn

© 2022 - 2024 — McMap. All rights reserved.