Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 326
Kit Avatar asked Aug 29 '10 04:08

Kit


People also ask

How do you iterate through a color in python?

How do you iterate colors in python? # Iterate through a string S = 'python' for x in S: print(x) # Prints p y t h o n. # Break the loop at 'blue' colors = ['red', 'green', 'blue', 'yellow'] for x in colors: if x == 'blue': break print(x) # Prints red green.

Which attribute of plot function is used to set the different colors of bars in bar chart?

The color attribute is used to set the color of the bars(maroon in this case).

How do I make each color bar different in matplotlib?

To set different colors for bars in a Bar Plot using Matplotlib PyPlot API, call matplotlib. pyplot. bar() function, and pass required color values, as list, to color parameter of bar() function.

How do you make a bar graph a different color in R?

To set colors for bars in Bar Plot drawn using barplot() function, pass the required color value(s) for col parameter in the function call. col parameter can accept a single value for color, or a vector of color values to set color(s) for bars in the bar plot.


2 Answers

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.

like image 124
ev-br Avatar answered Oct 22 '22 23:10

ev-br



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

like image 32
Dave Pena Avatar answered Oct 22 '22 23:10

Dave Pena