#3 Control color of barplots

This post aims to describe how to use colors on matplotlib barplots. First, let’s load libraries and create a fake dataset:

# libraries
import numpy as np
import matplotlib.pyplot as plt

# Make a fake dataset 
height = [3, 12, 5, 18, 45]
bars = ('A', 'B', 'C', 'D', 'E')
y_pos = np.arange(len(bars))

Now let’s study 3 examples of color utilization:

Uniform color using RGB
RGB is a way of making colors. You have to to provide an amount of red, green and blue + the transparency and it returns a color.

plt.bar(y_pos, height, color=(0.2, 0.4, 0.6, 0.6))
plt.xticks(y_pos, bars)

Different color for each bar

If you want to give different colors to each bar, just provide a list of color names to the color argument:

plt.bar(y_pos, height, color=['black', 'red', 'green', 'blue', 'cyan'])
plt.xticks(y_pos, bars)

Control color of border

The edgecolor argument allows to color the borders of barplots.

plt.bar(y_pos, height, color=(0.1, 0.1, 0.1, 0.1),  edgecolor='blue')
plt.xticks(y_pos, bars)


  • Sponsors

  • Leave a Reply

    Your email address will not be published.