How to Create Pie Chart from Pandas DataFrame?
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:
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

Explanation: 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.
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

Explanation: 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.
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

Explanation: 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.
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

Explanation: 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.
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

Explanation: 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.
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

Explanation: 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.