How To Plot A Pie Chart Using Python (9 Examples)

Pie charts are a great way to represent categorical data and provide comparative representations. They are also one of the most widely used graphs in data visualization. That is why in this article we will show you 9 ways that you can plot a pie chart using python programming.

When should you use a pie chart?

A pie chart expresses a part-to-whole relationship in your data through the use of “slices” where each slice represents one component and all slices added together equal the whole.

You may want to consider using a pie chart for data that you want to represent a general overview of the part-to-whole relationship in your data and comparing the segments of the data to indicate the precise sizes.

Dataset Used To Plot Our Graph

The dataset we will be using is from Kaggle, this dataset contains individual’s data of car insurance claims, which allows us to identify the type of personality and socio-economic levels of individuals that claim for car insurance

The dataset we will be using can be downloaded on the Kaggle website here:

If that link doesn’t work, you can directly download the CSV file here:

Using Matplotlib To Graph Pie Charts

Matplotlib is a widely used data visualization library that provides a wide range of charts. One chart they provide is a pie chart.

In this section, we will show you multiple ways you can visualize a pie chart using python’s matplotlib’s library.

1. Pie Chart

Matpotlib allows you to display a pie chart once you have declared all the necessary values and the corresponding labels.

To graph our pie chart we need to first import our data which I have done via the panda’s data library “read_csv” function.

Afterward, we need to manipulate our data to count the frequency of the unique attributes within our “INCOME” column, this can be done by using the dataframe’s function “value_counts()” to count the frequency.

We also need to create our labels.

This can be completed using the function “unique().tolist()” to identify the unique values within the “INCOME” column.

Pie chart using matplotlib
Matplotlib python code to display pie charts

2. Exploding Pie Chart

An exploding pie chart can be used to draw specific attention to a portion of the pie. In matplotlib to create a seperation of a slice we can use a variable called “explode” in the “plt.pie()” function of matplotlib.

To create the slide separation in our pie chart, we need to first declare a variable to specify the exact slice of the pie that requires seperation, this can be created using a tuple to specify the slice number and explosion size, where the first index is the first slice of the pie and so on.

Next, we can assign that variable to our attribute “explode” within our “plt.pie()” function.

Exploding Pie Chart Matplotlib
Exploding Pie chart python code for matplotlib

3. Donut Pie Chart

A donut chart is a variant of a pie chart, except it has a hole in the middle of the circle.  Since donut charts are hollowed out, there is no central point to attract your attention. This means your eyes travel around the circumference and judge each piece according to its length. Some may prefer the use of donut charts as it provides the user a better representation of the slices compared to a pie chart.

To plot a donut chart using matplotlib, you are basically plotting two things, one is the pie chart the other is the empty white circle to place within the pie chart. When you combine these two charts together it creates a donut chart.

To develop a circle we can use matplotlib’s circle function (“”) to create a white circle. Next, we need to use plt.gcf() to get our current pie chart and then combine it with our white circle by using “p.gca()” to combine the charts on the same axis, we then need to add our chart using “add_artist()”.

Donut Pie Chart Matplotlib

Using Plotly To Graph Pie Charts

Another way to graph our pie chart is using a python library called “plotly”. Plotly is a well-known python library because of its ability to provide more graphical tools and functions compared to Matplotlib.

4. Pie Chart

The only difference in creating the pie chart with plotly compared to matplotlib is the requirement to input our dataframe variable that reads our CSV file in our “px.pie” function.

Pie Chart Plotly
Pie chary pthon code for plotly

5. Exploding Pie Chart

To graph an exploding pie chart, we can use ploty’s package “graph_objects” which stores our pie chart function.

There is a slight difference in code between the pie chart(previously) and our current pie chart where our current chart requires the package “graph_objects” from plotly rather than “express”.

In terms of gathering the data from our CSV file the variables are the same, but with a slight change in the naming convention.

To make a slice explode in our pie chart there is an attribute called “pull” much like how matplotlib has an explosion attribute the listings are the same where first index specifies the first slice of the pie chart to explode and so on.

Exploding Pie Chart Plotly
Exploding Pie chart using plotly

6. Donut Pie Chart

To graph a donut chart we will also be using plotly’s “graph_objects” function.

Within the pie chart there’s an attribute called”hole” this adjusts the hole size of our donut chart, so by simply increasing the hole size, we can adjust the size of the gap within the middle of our donut chart.

Donut Pie Chart Plotly
Donut chart using plotly

7. Subplot Donut Pie Chart

Through the use of subplots we can use multiple charts to present a bigger picture of the data.

Luckily, plotly makes it very easy for us to make subplots through their package “plotly.subplots”.

To develop our subplot the process is very similar to our previous chart, but with the added condition where we need to import two columns of data and create two values and labels for our pie charts. Afterwards we need to specify the exact positioning to place our subplots.

Donut Pie chart Subplot Plotly
Subplot donut pie chart

8. Porportional Pie Charts

Plotly allows proportional pie charts, where charts that are on the same scale group can be represented by a size scale that shows its proportion compared to the other pie charts.

To give you a better representation of our proportional pie chart. Let’s take a look at the data we will be using.

In our current pie charts we are still analyzing the income levels however, with an added attribute which is the “VEHICLE_YEAR”, the vehicle year provides us the year which the vehicle is made, there are two unique data variables in this column which are “Before 2015” and “After 2015”.

If we apply the “VEHICLE_YEAR” to our pie charts, each of our pie chart sizes will adjust based on the amount of data we have in the pie chart “Before 2015” and “After 2015”.

So, by effectively splitting the pie charts we can identify the socio-economic levels of individuals who drive cars before 2015 and respectively after 2015.

To code our pie chart we need to first create our labels and values. The labels that will be used are the same for both pie charts which are our income levels. But with our values, we need two values one for vehicles before 2015 and the other for after 2015. To produce my values we needed to filter values based on “VEHICLE_YEAR” and count based on income levels.

Proportional Pie Chart Plotly
Proportional pie chart using plotly

9. Pyecharts: Nightingale Rose Pie Chart

Recently, “Nightingale rose map” has been a rising trend to create a pie chart this is due to its unique ability to represent data in hospital infections.

Nightingale’s Rose chart also referred to as “polar area chart” or “coxcomb chart” is a circular graph that combines elements of a radar chart and a column graph. By doing so, it provides a unique ability to dramatize key statistics within a pie chart slice.

Nightingale Rose Pie Chart
Nightingale rose pie chart using pyecharts


In this article, we have gone through 9 ways that you can graph a pie chart using python programming. Each Pie chart has its own unique ability to represent key statistics. We have also seen that each python libraries also have either more customizable features compared to others, therefore it is up to your needs to determine the right pie chart and python library to use in visualizing pie charts.