Pandas DataFrame plot.pie() Method

Rate this post

Preparation

Before any data manipulation can occur, four (4) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.
  • The Matplotlib library displays a visual graph of a plotted dataset.
  • The Scipy library allows users to manipulate and visualize the data.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

ip install matplotlib

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install scipy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy

DataFrame Plot Pie

The dataframe.plot.pie() method generates a Pie Chart based on a proportional representation of the numeric values in a column.

The syntax for this method is as follows:

DataFrame.plot.pie(**kwargs)
ParameterDescription
yThis parameter is the label/position of the column to plot.
**kwargsKeywords documented in DataFrame.plot().

For this example, Rivers Clothing plots its Quarterly Sales on a Pie Chart.

rivers_dict = {'Months': ['Jan','Aor','Jul','Oct'],
               'Sales':  [28744, 32600, 45700, 55900]}
df = pd.DataFrame(rivers_dict)

qtitle   = 'Rivers Clothing Quarterly Sales'
qlabels  = ['Q1','Q2','Q3','Q4']
qcolors  = ['#9932CC', '#8B008B', '#E6E6FA', '#9370DB']
qexplode = (0,0,0,0.2)

df.plot.pie(title=qtitle, y='Sales', figsize=(6,5), fontsize=9, 
	    labels=qlabels, colors=qcolors, 
            explode=qexplode, legend=False)
plt.show()
  • Line  [1] creates a dictionary of lists with quarterly sale details. This output saves to rivers_dict.
  • Line [2] creates a DataFrame from the dictionary created above.
  • Line [3] saves the title for the Pie chart to qtitle.
  • Line [4] saves the labels for the Pie chart to qlabels.
  • Line [5] saves the slices of the Pie chart to qcolors.
  • Line [6] saves the explode value (away from the main chart) to qexplode.
  • Line [7] creates a Pie chart using the parameters saved above.
  • Line [8] displays the Pie chart on-screen.

Output

The buttons on the bottom left can be used to further manipulate the chart.

💡 Note: Another way to create this chart is with the plot() method and the kind parameter set to the 'pie' option.

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.