Change the bar colors in a Matplotlib bar plot

Python

Matplotlib

Bar

Change bar colors

Luc B.

Python

Matplotlib

Bar Plot

Using different colors in a Matplotlib bar plot is a powerful technique for adding emphasis to a visualization. For example, colors can show additional categories beyond the ones represented by the bars themselves, they can draw attention to a particuallarly important data point, or they can simply add more visual distinction.

Code Example

Use the color parameter to plt.bar() to change the bar colors in Matplotlib. color can be a scalar value to change the color universally or an array to change the color of each bar individually.

import matplotlib.pyplot as plt

labels = ['Amy', 'Rob', 'Dimitri', 'Sarah', 'Brian']
values = [60, 56, 63, 75, 48]

# Change the bar colors here
plt.bar(labels, values, color=['red', 'blue', 'purple', 'green', 'lavender'])

plt.ylabel('Typing Speed (wpm)')
plt.show()

png

Valid color values are documented in the Matplotlib color reference.

More Examples

Change the Bar Color Universally

Change the bar color universally by passing a scalar value to color.

# Change the bar color here
plt.bar(labels, values, color='fuchsia')

plt.ylabel('Typing Speed (wpm)')
plt.show()

png