How to plot a pandas pie chart?

Today we will learn to build a basic pie plot using the pandas Data Analysis package. We typically build this type of quick plots as part of our exploratory data analysis.

Example DataFrame

In this example we will use some webinar attendance signup data

import pandas as pd
area = ['R', 'Java', 'R', 'Python', 'Python']
signups = [162, 126, 165, 178, 125]
data = pd.DataFrame(dict (area = area, signups = signups))

# group the data
webinar = data.groupby('area').agg(attendees = ('signups', sum))

Plot a simple pie graph

Rendering a simple graph is very simple using the DataFrame plot() method. We pass the following parameters:

  • kind – the plot type.
  • y – numeric value which we would like to measure
  • colormap (optional) – the color scheme for the chart.
  • title – the graph title.
webinar_pie = webinar.plot(kind= 'pie', y='attendees', colormap='Wistia')
webinar_pie.set_title('Attendees by expertise area');

Here’s our simple chart:

Show percentages in our pandas pie plot

To add percentages we will use the autopct parameter, as shown below:

webinar_pie = webinar.plot(kind= 'pie', y = 'attendees', autopct="%.1f%%", colormap='Wistia')
webinar_pie.set_title('Attendees by expertise area');
webinar_pie = campaign.groupby('area').agg(attendees = ('signups', sum));

Note: we can pass any function to the autopct parameter. We could use this in order to display not only percentages, but also labels on the pie chart.

Move the pie chart legend position

In the chart above you can see that the legend position is kind of overlapping the chart. We can fix it by using the legend method:

webinar_pie.legend(bbox_to_anchor= (1.01, 1));

Removing the y label from the chart

To get rid of the y label (in our case the word ‘attendees’ showing up at the left hands side of chart, simply add this line of code:


Putting it all together

Here’s the code for the complete chart:

webinar_pie = webinar.plot(kind= 'pie', y='attendees',autopct="%.0f%%", colormap='Wistia')
webinar_pie.set_title('Attendees by expertise area')
webinar_pie.legend(bbox_to_anchor= (1.01, 1))

And here’s the chart: