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 pie charts


Like bar charts, pie charts are also used in contexts where the color scheme might matter a lot. Pie chart coloring works mostly like in bar charts. In this recipe, we are going to see how to color pie charts with our own colors.

How to do it...

The function pyplot.pie() accepts a list of colors as an optional parameter, as shown in the following script:

import numpy as np
import matplotlib.pyplot as plt

values = np.random.rand(8)
color_set = ('.00', '.25', '.50', '.75')

plt.pie(values, colors = color_set)
plt.show()

The preceding script will produce the following pie chart:

How it works...

Pie charts accept a list of colors using the colors parameter (beware, it is colors, not color). However, the color list does not have as many elements as the input list of values. If there are less colors than values, then pyplot.pie() will simply cycle through the color list. In the preceding example, we gave a list of four colors to color a pie chart that consisted of...