Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Plotting Pandas DataFrames in Pie Charts using Matplotlib



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

Updated on: 2021-07-08T11:09:40+05:30

391 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements