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