Python Pandas DataFrame plot.pie() Function

Python Pandas DataFrame plot.pie() Function:

A pie chart (sometimes known as a circle chart) is a circular statistical visual that is divided into slices to show numerical proportion. The area of each slice in a pie chart is proportionate to the quantity it represents.

A pie plot is created using the DataFrame.plot.pie() function. For the given column, this function wraps matplotlib.pyplot.pie(). If no column reference is provided and subplots=True, a pie plot is generated for each numerical column independently.

Syntax:

DataFrame.plot.pie(y=None)

Parameters

y: This is optional. It indicates the label or position of the column to be plotted. If it is not provided, the subplots=True parameter must be used.

Return Value:

matplotlib.axes.Axes or an ndarray containing one matplotlib.axes.Axes per column when subplots=True is returned by the DataFrame.plot.pie() function.

Pandas DataFrame plot.pie() Function in Python

Example1

Approach:

  • Import pandas module using the import keyword.
  • Import pyplot function of the matplotlib module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe.
  • Plot the pie chart of the secondyear_marks of the students of the given dataframe using the dataframe.plot.pie() function.
  • Display the plot using the show() function of the matplotlib module.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Import pyplot function of the matplotlib module using the import keyword.
import matplotlib.pyplot as plt
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "firstyear_marks": [71, 60, 35, 85],
  "secondyear_marks": [80, 40, 25, 90]},
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Plot the pie chart of the secondyear_marks of the students of the
# given dataframe using the dataframe.plot.pie() function 
data_frme.plot.pie(y='secondyear_marks')

# Display the plot using the show() function of the matplotlib module 
plt.show()

Output:

The given Dataframe:
        firstyear_marks  secondyear_marks
virat                71                80
nick                 60                40
jessy                35                25
sindhu               85                90

Example2

When no column reference is given and the subplots=True parameter is utilised, a pie plot is generated for each numerical column separately.

Approach:

  • Import pandas module using the import keyword.
  • Import pyplot function of the matplotlib module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe.
  • Plot the pie chart of all the numerical columns of the given dataframe using the dataframe.plot.pie() function by passing subplots=True to it and adjust the size of the plot using the figsize() argument.
  • Here it plots both the firstyear_marks, secondyear_marks columns.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Import pyplot function of the matplotlib module using the import keyword.
import matplotlib.pyplot as plt
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "firstyear_marks": [71, 60, 35, 85],
  "secondyear_marks": [80, 40, 25, 90]},
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Plot the pie chart of all the numerical columns of the given dataframe 
# using the dataframe.plot.pie() function by passing subplots=True to it
# and adjust the size of the plot using the figsize() argument.
# Here it plots both the firstyear_marks, secondyear_marks columns
data_frme.plot.pie(subplots=True, figsize=(12, 8))

# Display the plot using the show() function of the matplotlib module 
plt.show()

Output:

The given Dataframe:
        firstyear_marks  secondyear_marks
virat                71                80
nick                 60                40
jessy                35                25
sindhu               85                90