Book Image

matplotlib Plotting Cookbook

By : Alexandre Devert
Book Image

matplotlib Plotting Cookbook

By: Alexandre Devert

Overview of this book

Table of Contents (15 chapters)
matplotlib Plotting Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using custom colors for bar charts


Bar charts are used a lot in web pages and presentations where one often has to follow an established color scheme. Thus, a good control on their colors is a must. In this recipe, we are going to see how to color a bar chart with our own colors.

How to do it...

In Chapter 1, First Steps, we have already seen how to make bar charts. Controlling which colors are used works the same as it does for curves plots and scatter plots, that is, through an optional parameter. In this example, we load the age pyramid of a country's population from a file as follows:

import numpy as np
import matplotlib.pyplot as plt

women_pop = np.array([5., 30., 45., 22.])
men_pop     = np.array([5., 25., 50., 20.])

X = np.arange(4)
plt.barh(X, women_pop, color = '.25')
plt.barh(X, -men_pop, color = '.75')

plt.show()

The preceding script shows one bar chart with the age repartition for men and another bar chart for women. Women appear in dark gray, while men appear in light gray, as...