Newb question - multiple colors in bar chart

Good morning,

I hate to ask such a newbie question but I am a newb with matplotlib :slight_smile:

I want to create a bar chart with, for example, 6 "bars". I want the first
3 bars to be one color and the second 3 bars to be another color. I'm
not entirely sure how to do this - any suggestions?

Alternatively, I would like to create 6 bars where every other bar is
a different color. For example, gray, red, gray, red, gray, red. Any
suggestions on this?

TIA!

Jeff

Hi Jeff,

you can do something like the following and specify the color for each bar by
providing a list of colors for the keyword argument 'color'.

import numpy as np
import matplotlib.pyplot as plt

# generate some data
left = np.arange(6)
height = np.random.uniform(size=6)
# plot 3 blue and 3 red bars:
plt.bar(left, height, color=['blue']*3+ ['red']*3)
plt.show()

You second way could be provided by the color list

['red', '0.5']*3 = ['red', '0.5', 'red', '0.5', 'red', '0.5']

Kind regards,
Matthias

···

On Saturday 23 January 2010 16:36:36 Jeff Layton wrote:

Good morning,

I hate to ask such a newbie question but I am a newb with matplotlib :slight_smile:

I want to create a bar chart with, for example, 6 "bars". I want the first
3 bars to be one color and the second 3 bars to be another color. I'm
not entirely sure how to do this - any suggestions?

Alternatively, I would like to create 6 bars where every other bar is
a different color. For example, gray, red, gray, red, gray, red. Any
suggestions on this?

TIA!

Jeff