How to generate PIE plot in Python?

How to generate PIE plot in Python?

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.

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();

  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

Relevant Projects

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.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.