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, ...

Get matplotlib Plotting Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.