How to Create Pie Chart from Pandas DataFrame?
Last Updated :
19 Dec, 2021
In this article, we will discuss how to create a Pie chart from Pandas dataframe using Python.
The data in a circular graph is represented by a pie chart, which is a form of a graph. In research, engineering, and business, it is frequently utilized. The segments of the pie depict the data's relative strength and are a sort of graphical representation of data. A list of categories and numerical variables is required for a pie chart. The phrase "pie" refers to the entire, whereas "slices" refers to the individual components of the pie. It is divided into segments and sectors, with each segment and sector representing a piece of the whole pie chart (percentage). All of the data adds up to 360 degrees. The pie's entire worth is always 100 percent.
Let us first create a simple Pie chart.
Simple Pie chartÂ
For this first, all required modules are imported and a dataframe is initialized. To plot a pie chart plot() function is used and the kind attribute is set to pie.
Syntax:
plot(kind='pie')
Example: A simple pie chart
Python3
import pandas as pd
# DataFrame of each student and the votes they get
dataframe = pd.DataFrame({'Name': ['Aparna', 'Aparna', 'Aparna',
'Aparna', 'Aparna', 'Juhi',
'Juhi', 'Juhi', 'Juhi', 'Juhi',
'Suprabhat', 'Suprabhat', 'Suprabhat',
'Suprabhat', 'Suprabhat'],
'votes_of_each_class': [12, 9, 17, 19, 20,
11, 15, 12, 9, 4,
22, 19, 17, 19, 18]})
# Plotting the pie chart for above dataframe
dataframe.groupby(['Name']).sum().plot(kind='pie', y='votes_of_each_class')
Output:

Pie Chart as Percentage
To add percentage autopct attribute is set to an appropriate value, this automatically adds percentages to each section.
Syntax:
plot(kind='pie', autopct)
Example: Adding percentages to pie chart
Python3
import pandas as pd
# DataFrame of each student and the votes they get
dataframe = pd.DataFrame({'Name': ['Aparna', 'Aparna', 'Aparna',
'Aparna', 'Aparna', 'Juhi',
'Juhi', 'Juhi', 'Juhi', 'Juhi',
'Suprabhat', 'Suprabhat',
'Suprabhat', 'Suprabhat',
'Suprabhat'],
'votes_of_each_class': [12, 9, 17, 19,
20, 11, 15, 12,
9, 4, 22, 19, 17,
19, 18]})
# Plotting the pie chart for above dataframe
dataframe.groupby(['Name']).sum().plot(
kind='pie', y='votes_of_each_class', autopct='%1.0f%%')
Output:

Defining Colors in a Pie Chart
To add colors to a pie chart, the colors attribute is set to an appropriate list of colors.
Syntax:
plot(kind='pie', colors)
Example: Adding colors to pie chartÂ
Python3
import pandas as pd
# DataFrame of each student and the votes they get
dataframe = pd.DataFrame({'Name': ['Aparna', 'Aparna', 'Aparna',
'Aparna', 'Aparna', 'Juhi',
'Juhi', 'Juhi', 'Juhi', 'Juhi',
'Suprabhat', 'Suprabhat',
'Suprabhat', 'Suprabhat',
'Suprabhat'],
'votes_of_each_class': [12, 9, 17, 19,
20, 11, 15, 12,
9, 4, 22, 19, 17,
19, 18]})
# Defining colors for the pie chart
colors = ['pink', 'silver', 'steelblue']
# Plotting the pie chart for above dataframe
dataframe.groupby(['Name']).sum().plot(
kind='pie', y='votes_of_each_class',
autopct='%1.0f%%', colors=colors)
Output:

Explode effect in Pie Chart
Exploding a pie chart means breaking it into its segments. For this, we use explode attribute and set it to an appropriate value.
Syntax:
plot(kind='pie', explode)
Example: Exploding a pie chart
Python3
import pandas as pd
# DataFrame of each student and the votes they get
dataframe = pd.DataFrame({'Name': ['Aparna', 'Aparna', 'Aparna',
'Aparna', 'Aparna', 'Juhi',
'Juhi', 'Juhi', 'Juhi', 'Juhi',
'Suprabhat', 'Suprabhat',
'Suprabhat', 'Suprabhat',
'Suprabhat'],
'votes_of_each_class': [12, 9, 17, 19,
20, 11, 15, 12,
9, 4, 22, 19, 17,
19, 18]})
# Defining colors for the pie chart
colors = ['pink', 'silver', 'steelblue']
# Define the ratio of gap of each fragment in a tuple
explode = (0.05, 0.05, 0.05)
# Plotting the pie chart for above dataframe
dataframe.groupby(['Name']).sum().plot(
kind='pie', y='votes_of_each_class', autopct='%1.0f%%',
colors=colors, explode=explode)
Output :

Shadow effect in Pie Chart
Shadow adds an extra dimension to our pie chart, for this just set the shadow attribute to True.
Syntax:
plot(kind='pie', shadow=True)
Example: Shadow effect in pie chartÂ
Python3
import pandas as pd
# DataFrame of each student and the votes they get
dataframe = pd.DataFrame({'Name': ['Aparna', 'Aparna', 'Aparna',
'Aparna', 'Aparna', 'Juhi',
'Juhi', 'Juhi', 'Juhi', 'Juhi',
'Suprabhat', 'Suprabhat',
'Suprabhat', 'Suprabhat',
'Suprabhat'],
'votes_of_each_class': [12, 9, 17, 19,
20, 11, 15, 12,
9, 4, 22, 19, 17,
19, 18]})
# Plotting the pie chart for above dataframe
# and implementing shadow effect
dataframe.groupby(['Name']).sum().plot(
kind='pie', y='votes_of_each_class', autopct='%1.0f%%', shadow=True)
Output:

Setting Start Angle in Pie Chart
The start Angle implies that we can rotate the pie chart according to the degree angle we specified. For this startangle attribute is set to the appropriate value.
Syntax:
plot(kind='pie', startangle)
Example: Setting a start angle in Pie chartÂ
Python3
import pandas as pd
# DataFrame of each student and the votes they get
dataframe = pd.DataFrame({'Name': ['Aparna', 'Aparna', 'Aparna',
'Aparna', 'Aparna', 'Juhi',
'Juhi', 'Juhi', 'Juhi', 'Juhi',
'Suprabhat', 'Suprabhat',
'Suprabhat', 'Suprabhat',
'Suprabhat'],
'votes_of_each_class': [12, 9, 17, 19,
20, 11, 15, 12,
9, 4, 22, 19, 17,
19, 18]})
# Plotting the pie chart for above dataframe
# and rotating the pie chart by 60 degrees
dataframe.groupby(['Name']).sum().plot(
kind='pie', y='votes_of_each_class', autopct='%1.0f%%', startangle=60)
Output :
Similar Reads
How to Create Boxplot from Pandas DataFrame?
A box plot (or whisker plot) is a statistical graph that shows the minimum, first quartile (Q1), median, third quartile (Q3) and maximum values of a dataset. It helps analyze data spread, skewness and outliers and is widely used in data visualization. In this article you'll learn how to create box p
2 min read
How to Create a Histogram from Pandas DataFrame?
A histogram is a graph that displays the frequency of values in a metric variable's intervals. These intervals are referred to as "bins," and they are all the same width. We can create a histogram from the panda's data frame using the df.hist() function. Syntax: DataFrame.hist(column=None, by=None,
2 min read
How to Plot Multiple Series from a Pandas DataFrame?
In this article, we will discuss how to plot multiple series from a dataframe in pandas. Series is the range of the data  that include integer points we cab plot in pandas dataframe by using plot() function Syntax: matplotlib.pyplot(dataframe['column_name']) We can place n number of series and we ha
2 min read
How to Create a Dynamic Pie Chart in Excel?
In Excel, Pie-chart is a graphical representation of different sections or sectors of a circle based on the proportion, it holds from the complete quantity. Pie-charts are generally categorized into two types: Static Pie-chart: A pie-chart created with static or fixed input values is known to be a s
3 min read
Python | Pandas dataframe.aggregate()
Dataframe.aggregate() function is used to apply some aggregation across one or more columns. Aggregate using callable, string, dict or list of string/callables. The most frequently used aggregations are:sum: Return the sum of the values for the requested axismin: Return the minimum of the values for
2 min read
Create empty dataframe in Pandas
The Pandas Dataframe is a structure that has data in the 2D format and labels with it. DataFrames are widely used in data science, machine learning, and other such places. DataFrames are the same as SQL tables or Excel sheets but these are faster in use.Empty DataFrame could be created with the help
1 min read
How to Plot a Dataframe using Pandas
Pandas plotting is an interface to Matplotlib, that allows to generate high-quality plots directly from a DataFrame or Series. The .plot() method is the core function for plotting data in Pandas. Depending on the kind of plot we want to create, we can specify various parameters such as plot type (ki
8 min read
How to Create a Line Chart for Comparing Data in Excel?
Excel is powerful data visualization and data management tool which can be used to store, analyze, and create reports on large data. It can be used to visualize data using a graph plot. In excel, we can plot different kinds of graphs like line graphs, bar graphs, etc., to visualize or analyze the tr
2 min read
How to export Pandas DataFrame to a CSV file?
Let us see how to export a Pandas DataFrame to a CSV file. We will be using the to_csv() function to save a DataFrame as a CSV file. DataFrame.to_csv() Syntax : to_csv(parameters) Parameters : path_or_buf : File path or object, if None is provided the result is returned as a string. sep : String of
3 min read
Concatenate two columns of Pandas dataframe
Let's discuss how to Concatenate two columns of dataframe in pandas python. We can do this by using the following functions : concat() append() join() Example 1 : Using the concat() method. Python3 1== # importing the module import pandas as pd # creating 2 DataFrames location = pd.DataFrame({'area'
2 min read