How to generate PIE plot in Python?

This recipe helps you generate PIE plot in Python

Recipe Objective

Ploting a visual figure of data distribution helps us a lot in analysing a data.

So this is the recipe on how we can generate PIE plot in Python.

Master the Art of Data Cleaning in Machine Learning

Step 1 - Import the library

import pandas as pd import matplotlib.pyplot as plt

We have imported matplotlib.pyplot and pandas which is needed.

Step 2 - Creating DataFrame

raw_data = {"officer_name": ["Jason", "Molly", "Tina", "Jake", "Amy"], "jan_arrests": [4, 24, 31, 2, 3], "feb_arrests": [25, 94, 57, 62, 70], "march_arrests": [5, 43, 23, 23, 51]} df = pd.DataFrame(raw_data, columns = ["officer_name", "jan_arrests", "feb_arrests", "march_arrests"]) print(); print(df)

We have created a dictionary with various features and we have passed it through pd.DataFrame to create a dataset.

Step 3 - Ploting Pie Plot

We have created a new feature which will store the sum of all the data of which we want to create Pie Plot. df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print(df) We have made an array of colour code and used it in ploting pie chart. We have ploted Pie cart using Pli.pie by passing the data of which we want to plot it. colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie(df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=(0, 0, 0, 0, 0.15), startangle=90, autopct="%1.1f%%") Finally we are printing the Pie Chart plt.axis("equal") plt.tight_layout(); plt.show()

  officer_name  jan_arrests  feb_arrests  march_arrests
0        Jason            4           25              5
1        Molly           24           94             43
2         Tina           31           57             23
3         Jake            2           62             23
4          Amy            3           70             51

  officer_name  jan_arrests  feb_arrests  march_arrests  total_arrests
0        Jason            4           25              5             34
1        Molly           24           94             43            161
2         Tina           31           57             23            111
3         Jake            2           62             23             87
4          Amy            3           70             51            124

Download Materials

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.