GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • All
  • Best
  • Libs
  • Learn

This view

  • Original page
  • New address

GeezerVison is showing python-graph-gallery.com — Pie charts with Python and Matplotlib

Pie charts with Python and Matplotlib

logo of a chart:Pie

This post describes how to build a pie chart with Python. It starts with basic examples and then describes the most usual customisation like adding proper labels and colors.

Pie Chart sectionAbout this chart

Most basic pie chart with Matplotlib

The matplotlib library allows to easily build a pie chart thanks to its pie() function. It expects at the very least some data input. This data must be an array of numbers as in the example below:

Pie charts with Python and Matplotlib: Most basic pie chart with Matplotlib

# library
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (20,5)
# create random data
values=[12,11,3,30]
 
# Create a pieplot
plt.pie(values);
plt.show();

Add labels

The above pie chart is a good start but is definitely not very insightful. The first thing we miss here are labels.

Labels are added thanks to the labels parameter that expect an array of strings. You also problably want to play with the labeldistance parameter that controls the distance between the circle edge and the label itself. (1= exactly on it, use something bigger to have the label further.)

Pie charts with Python and Matplotlib: Add labels

# library
import matplotlib.pyplot as plt
 
# create random data
names='groupA', 'groupB', 'groupC', 'groupD',
values=[12,11,3,30]
 
# Create a pieplot
plt.pie(size_of_groups)

# Label distance: gives the space between labels and the center of the pie
plt.pie(values, labels=names, labeldistance=1.15);
plt.show();
Dataviz caveat card: What's wrong with pie charts?

⚠️ Avoid the 61 most common dataviz mistakes

The dataviz caveats I see the most, as a deck of 61 cards: a chart that is not ideal, and a better alternative. Free.

I respect your privacy. Unsubscribe anytime.

Customize wedges

It is pretty common to customize wedges to get some blank spaces around them. This is doable thanks to the wedgeprops parameter. This parameter is an object that accept 2 properties: linewidth and edgecolor

Pie charts with Python and Matplotlib: Customize wedges

## Same chart as above but with specific wedgeprops option:
plt.pie(values, labels=names, labeldistance=1.15, wedgeprops = { 'linewidth' : 3, 'edgecolor' : 'white' });

Customize colors

You can change the color palette in use thanks to the color parameter that expects an array of color. See the dedicated section of the gallery for more tips on color with matplotlib.

Pie charts with Python and Matplotlib: Customize colors

# Create a set of colors
colors = ['#4F6272', '#B7C3F3', '#DD7596', '#8EB897']

# Use it thanks to the color argument
plt.pie(values, labels=names, labeldistance=1.15, wedgeprops = { 'linewidth' : 1, 'edgecolor' : 'white' }, colors=colors);

Related posts

Basic pie plot using pandas

Basic pie plot using pandas

Related chart types

logo of a chart:Tree

Treemap

logo of a chart:Venn

Venn Diagram

logo of a chart:Doughnut

Donut

logo of a chart:Pie

Pie Chart

logo of a chart:Dendrogram

Dendrogram

logo of a chart:CircularPacking

Circular Packing

logo of a chart:Waffle2

Waffle

300 projects

💡 Out of dataviz inspiration?

I have spent years collecting the most beautiful data visualizations out there. Get the list of my top 300 dataviz projects, hand-picked, for free.

I respect your privacy. Unsubscribe anytime.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.