How to create a pie chart using Matplotlib

Pie chart created with Matplotlib
Pie chart created with Matplotlib

Creating a pie chart is easy using Matplotlib. A simple pie chart can be created by passing the values representing each of the slices of the pie to the matplotlib.pyplot.pie method. For example:

Typically, you want some additions to the simple pie chart:

1. The pie chart is oval by default. To make it a circle, call matplotlib.pyplot.axis("equal")

2. To show labels of each pie slice, pass them as a list of strings to the labels parameter of the matplotlib.pyplot.pie method.

3. To show the percentage of each pie slice, pass an output format to the autopct parameter of the matplotlib.pyplot.pie method.

4. To display a title for the pie chart, call the matplotlib.pyplot.title method.

The pie chart shown above was created by these simple additions. The code is below:

Tried with: Matplotlib 1.3.0 and Ubuntu 12.04 LTS

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.