plotly Pie & Donut Chart in Python (4 Examples)

 

Hi! This tutorial will show you how to make interactive pie and donut charts in plotly in the Python programming language.

Here is a quick overview of this tutorial:

Let’s dive into it!

 

What is A Pie Chart & A Donut Chart?

A pie chart is a circular visual representation that displays different categories as slices, proportional to their corresponding values within a whole. It offers a quick and intuitive way to show data distribution and relative proportions.

A donut chart, on the other hand, is a variation of the pie chart with a hole in the center. It presents data in segments like a pie chart while also allowing for additional information or labels to be placed in the central hole, enhancing data representation and context.

In order to build a pie chart and a donut chart with plotly, we will need to install and import the library.

 

Install & Import plotly

To install and import the Python plotly library, run the lines of code below in your favorite Python coding IDE:

# install plotly
pip install plotly
 
# import plotly
import plotly.express as px

With plotly now installed and imported into our Python programming environment, we can now go on to use its plot-building functions to build a pie chart and a donut chart.

However, we will need to load the dataset we will visualize as a pie chart and a donut chart.
 

Create Example Dataset

We will use the popular tips dataset that comes pre-loaded in plotly as our example dataset. Therefore, run the code below to load and view the first five rows of the dataset:

df = px.data.tips()
 
df.head()
 
#  total_bill	tip	sex	smoker	day	time	size
#0	16.99	1.01	Female	No	Sun	Dinner	2
#1	10.34	1.66	Male	No	Sun	Dinner	3
#2	21.01	3.50	Male	No	Sun	Dinner	3
#3	23.68	3.31	Male	No	Sun	Dinner	2
#4	24.59	3.61	Female	No	Sun	Dinner	4

Now that the dataset is loaded, we can now build visualizations.
 

Example 1: Build Basic Pie Chart

In this first example, we will build a basic pie chart. Therefore, run the code below to build the pie chart:

fig = px.pie(df, values="total_bill", names="day")
 
fig.show()

In the example above, we built a simple pie chart. In the code, we simply passed the DataFrame to the px.pie() method, and also defined the values = and names = arguments to build the pie chart.
 

Example 2: Adjust Colors of Pie Chart Sectors

In this second example, we will modify the color of the sectors of the pie chart. So, run the code below to do so:

fig = px.pie(df, values="total_bill", names="day",color_discrete_sequence=px.colors.sequential.Blackbody)
 
fig.show()

As you can see, we have changed the colors of the sectors of the pie chart. The code remains the same, except that we introduced a new argument color_discrete_sequence = and passed a color scale to it. You can check here for other color scales you can play with.
 

Example 3: Map Colors to Pie Chart Sectors

Here, we will explicitly map discrete colors in the pie chart. Run the code below to do so:

fig = px.pie(df, values="total_bill", names="day", color="day",
             color_discrete_map={"Thur":"#B39DDB",
                                 "Fri":"#9575CD",
                                 "Sat":"#7E57C2",
                                 "Sun":"#673AB7"})
 
fig.show()

In the above example, we passed a dictionary containing the days as keys and hex color codes as values to the color_discrete_map = argument, which we defined in px.pie().

With this, you can easily map discrete colors to the sectors of a pie chart.
 

Example 4: Make Donut Chart

In this last example, we will turn the basic pie chart into a donut chart by introducing and defining a new argument. Run the code below:

fig = px.pie(df, values="total_bill", names="day", hole=.5)
 
fig.show()

In order to create the above donut chart, all we needed to do was simply define the hole = argument by passing a decimal to it, which will determine how big or small we want the central hole to be.

In the example above, we passed .5 to the argument. You can play around with this parameter according to your need as you build your donut chart.

All the other styling we did in the previous examples can also be applied to the donut chart.
 

Video, Further Resources & Summary

Do you need more explanations on how to make a plotly pie chart and donut in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain how to make a plotly pie chart and donut in Python.

 

The YouTube video will be added soon.

 

You can check out these other articles for more detailed examples and videos of these popular charts in plotly using the Python programming language:

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top