Plotting Pandas DataFrames in Pie Charts using Matplotlib

PythonServer Side ProgrammingProgramming

To plot Pandas data frames in Pie charts using Matplotlib, we can take the following steps −

  • Set the figure size and adjust the padding between and around the subplots.
  • Make a dataframe of two-dimensional, size-mutable, potentially heterogeneous tabular data.
  • Plot the dataframe with activities index using pie() method
  • To display the figure, use show() method.

Example

import pandas as pd
from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

df = pd.DataFrame({'activities': ['sleep', 'exercise', 'work', 'study'],
                                    'hours': [8, 1, 9, 6]})

df.set_index('activities').plot.pie(y='hours', legend=False,
                                    autopct='%1.1f%%')

plt.show()

Output

raja
Published on 08-Jul-2021 11:09:07
Advertisements