- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
In Matplotlib, show the percentage or proportional data where each slice of pie represents a category
In this article, we can create a pie chart to show our daily activities, i.e., sleeping, eating, working, and playing. Using plt.pie() method, we can create a pie chart with the given different data sets for different activities.
Steps
Create a list of days, i.e., [1, 2, 3, 4, 5]. Similarly, make lists for sleeping, eating, playing, and working. There is an activities list that keeps “sleeping”, “eating”, “working” and “playing”.
Make a list of colors.
Use plt.pie() method to draw the pie chart, where slices, activities, colors as cols, etc. are passed.
Set a title for the axes, i.e., “Pie Chart”.
To show the figure use plt.show() method.
Example
import matplotlib.pyplot as plt days = [1, 2, 3, 4, 5] sleeping = [7, 8, 6, 11, 7] eating = [2, 3, 4, 3, 2] working = [7, 8, 7, 2, 2] playing = [8, 5, 7, 8, 13] slices = [7, 2, 3, 13] activities = ['sleeping', 'eating', 'working', 'playing'] cols = ['c', 'm', 'r', 'b'] plt.pie(slices, labels=activities, colors=cols, startangle=90, shadow=True, explode=(0, 0.1, 0, 0), autopct='%1.1f%%') plt.title('Pie Plot') plt.show()
Output
When we execute the code, it will produce the following output −
- Related Articles
- How to find the percentage of each category in an R data frame column?
- How to find the percentage of each category in a data.table object column in R?
- How to find the count of each category in an R data frame column?
- How to find the percentage of zeros in each column of a data frame in R?
- How to set a title above each marker which represents a same label in Matplotlib?
- Conditional removal of labels in Matplotlib pie chart
- How to plot pie-chart with a single pie highlighted with Python Matplotlib?
- How to plot a nested pie chart in Matplotlib?
- How to extract the closest value to a certain value in each category in an R data frame?
- How to avoid overlapping of labels & autopct in a Matplotlib pie chart?
- How to find the percentage of missing values in each column of an R data frame?
- How to display pie charts in Matplotlib Python?
- Plotting Pandas DataFrames in Pie Charts using Matplotlib
- How to add a legend to a Matplotlib pie chart?
- How to find the count of each category in a data.table object column in R?

Advertisements