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

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

Build a Speech-Text Transcriptor with Nvidia Quartznet Model
In this Deep Learning Project, you will leverage transfer learning from Nvidia QuartzNet pre-trained models to develop a speech-to-text transcriptor.

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.