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

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

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.

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.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

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

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.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.