- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Conditional removal of labels in Matplotlib pie chart
To remove labels from a Matplotlib pie chart based on a condition, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create a Pandas dataframe of wwo-dimensional, size-mutable, potentially heterogeneous tabular data.
- Plot a pie chart, using pie() method with conditional removal of labels, such that if %age value is greater than 25, then only keep labels, otherwise remove them.
- To display the figure, use show() method.
Example
import pandas as pd from matplotlib import pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create a Pandas dataframe df = pd.DataFrame( { 'activities': ['sleep', 'exercise', 'work', 'study'], 'hours': [8, 1, 9, 4] } ) # Pie chart with conditional removal of labels df.set_index('activities').plot.pie(y='hours', legend=False, autopct=lambda p: format(p, '.2f') if p > 25 else None) plt.show()
Output
It will produce the following output
Notice that the pie chart shows the labels only when the percentage of value is greater than 25 (as per the condition). Since the values of "exercise" and "study" are less then 25, the pie chart doesn't reflect those labels.
- Related Articles
- How to avoid overlapping of labels & autopct in a Matplotlib pie chart?
- How to create pie chart in base R with labels?
- How to make the labels of a JavaFX Pie chart invisible?
- How to plot a nested pie chart in Matplotlib?
- How to plot pie-chart with a single pie highlighted with Python Matplotlib?
- How to have actual values in Matplotlib Pie Chart displayed?
- Adding value labels on a matplotlib bar chart
- How to add a legend to a Matplotlib pie chart?
- Python - Plot a Pie Chart for Pandas Dataframe with Matplotlib?
- How can I generate more colors on a pie chart in Matplotlib?
- How to plot a Bar Chart with multiple labels in Matplotlib?
- How to change autopct text color to be white in a pie chart in Matplotlib?
- Explain: How to draw Pie chart?
- How to use pie chart graph in android?
- How to create pie chart in base R?

Advertisements