How to Create Pie Chart from Pandas DataFrame?
Last Updated :
28 May, 2025
A pie chart is a circular statistical graphic divided into slices to illustrate numerical proportions. Each slice represents a category's contribution to the whole, typically expressed as a percentage of 100%. Pie charts are widely used in research, engineering, business analytics and data visualization tasks to show parts-to-whole relationships.
Before you begin, install the required libraries:
pip install pandas matplotlib
Example:
Python
import pandas as pd
df = pd.DataFrame({
'Name': ['Aparna'] * 5 + ['Juhi'] * 5 + ['Suprabhat'] * 5,
'votes_of_each_class': [12, 9, 17, 19, 20, 11, 15, 12, 9, 4, 22, 19, 17, 19, 18]
})
df.groupby('Name').sum().plot(kind='pie', y='votes_of_each_class')
Output
Simple pie chartExplanation: The code starts by creating lists of names repeated five times and their corresponding vote counts, which are combined into a table using pd.DataFrame(). Then, groupby('Name') groups the data by each name and sum() totals the votes per group. Finally, plot(kind='pie', y='votes_of_each_class') generates a pie chart.
Customizing the Pie Chart Appearance
To make your pie charts more engaging and easier to understand, you can customize their look in several ways. Adding labels, changing colors or highlighting slices can help emphasize important data. Let’s explore some simple tweaks to make your charts stand out.
1. Adding percentage to the pie chart
One of the most useful enhancements in a pie chart is the ability to display percentage values directly on each slice. This helps viewers quickly understand how much each category contributes to the total without needing to calculate it manually.
Python
import pandas as pd
df = pd.DataFrame({
'Name': ['Aparna'] * 5 + ['Juhi'] * 5 + ['Suprabhat'] * 5,
'votes_of_each_class': [12, 9, 17, 19, 20, 11, 15, 12, 9, 4, 22, 19, 17, 19, 18]
})
df.groupby('Name').sum().plot(
kind='pie', y='votes_of_each_class', autopct='%1.0f%%')
Output
Add Percentage LabelsExplanation: Adding the autopct='%1.0f%%' parameter displays the percentage value on each slice, making it easier to see how much each category contributes to the total.
2. Customizing colors
In pie charts, using custom colors for each slice helps differentiate categories visually. You can pass a list of colors using the colors parameter to make your chart more vibrant or match your brand's theme.
Python
import pandas as pd
df = pd.DataFrame({
'Name': ['Aparna'] * 5 + ['Juhi'] * 5 + ['Suprabhat'] * 5,
'votes_of_each_class': [12, 9, 17, 19, 20, 11, 15, 12, 9, 4, 22, 19, 17, 19, 18]
})
a = ['pink', 'silver', 'steelblue']
df.groupby('Name').sum().plot(
kind='pie', y='votes_of_each_class',
autopct='%1.0f%%', colors=a)
Output
Customize Slice ColorsExplanation: The colors parameter allows you to specify a list of colors for the slices, helping to visually distinguish different categories and enhance the chart’s appearance.
3. Exploding Pie slices
Exploding slices refers to slightly separating one or more slices from the pie for emphasis. This feature is helpful when you want to highlight specific segments in your chart. Use the explode parameter with a tuple of values indicating how far each slice should be pushed outward.
Python
import pandas as pd
df = pd.DataFrame({
'Name': ['Aparna'] * 5 + ['Juhi'] * 5 + ['Suprabhat'] * 5,
'votes_of_each_class': [12, 9, 17, 19, 20, 11, 15, 12, 9, 4, 22, 19, 17, 19, 18]
})
a = ['pink', 'silver', 'steelblue'] # colors
b = (0.05, 0.05, 0.05) # Slightly separate all slices
df.groupby('Name').sum().plot(
kind='pie', y='votes_of_each_class',
autopct='%1.0f%%', colors=a, explode=b)
Output
Explode Pie SlicesExplanation: The explode parameter accepts a tuple that pushes slices outward by the specified fraction, which helps emphasize certain slices by slightly separating them from the pie.
4. Adding a shadow
Adding a shadow gives your pie chart a 3D visual effect that can enhance its appearance. It helps make the chart more visually engaging, especially when presented in dashboards or reports. This can be done using the shadow=True parameter.
Python
import pandas as pd
df = pd.DataFrame({
'Name': ['Aparna'] * 5 + ['Juhi'] * 5 + ['Suprabhat'] * 5,
'votes_of_each_class': [12, 9, 17, 19, 20, 11, 15, 12, 9, 4, 22, 19, 17, 19, 18]
})
df.groupby('Name').sum().plot(
kind='pie', y='votes_of_each_class',
autopct='%1.0f%%', shadow=True)
Output
Shadow EffectExplanation: Setting shadow=True adds a subtle shadow beneath the pie chart, giving it a 3D effect and making the chart visually more appealing.
5. Rotating the pie chart with a start angle
Sometimes, you may want to rotate the pie chart to start from a different angle for aesthetic or emphasis reasons. The startangle parameter allows you to rotate the chart so that the first slice begins at a specified angle.
Python
import pandas as pd
df = pd.DataFrame({
'Name': ['Aparna'] * 5 + ['Juhi'] * 5 + ['Suprabhat'] * 5,
'votes_of_each_class': [12, 9, 17, 19, 20, 11, 15, 12, 9, 4, 22, 19, 17, 19, 18]
})
df.groupby('Name').sum().plot(
kind='pie', y='votes_of_each_class',
autopct='%1.0f%%', startangle=60)
Output
Rotated Pie ChartExplanation: The startangle parameter rotates the pie chart so that the first slice begins at the specified angle (in degrees). This can improve aesthetics or help highlight a specific slice.
Related Articles
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