- 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
How to change Bar Chart values to percentages in Matplotlib?
To change bar chart values to percentage in matplotlib, we can take the following steps
Steps
Set the figure size and adjust the padding between and around the subplots.
Make a list of frequencies.
Create a new figure or activate an existing figure.
Make a bar plot using bar() method.
Iterate the bar plot and find the height of each patch and use annotate() method to put values in percentages.
To display the figure, use Show() method.
Example
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True frequencies = [7, 8, 5, 3, 6] plt.figure() p1 = plt.bar(np.arange(len(frequencies)), frequencies) for rect1 in p1: height = rect1.get_height() plt.annotate( "{}%".format(height),(rect1.get_x() + rect1.get_width()/2, height+.05),ha="center",va="bottom",fontsize=15) plt.show()
Output
It will produce the following output −
- Related Articles
- How to display stacked bar chart using matplotlib in Python?
- How to Create a Diverging Stacked Bar Chart in Matplotlib?
- How to display percentage above a bar chart in Matplotlib?
- How to remove gaps between bars in Matplotlib bar chart?
- How to plot a Bar Chart with multiple labels in Matplotlib?
- Horizontal stacked bar chart in Matplotlib
- How to change the order of bars in bar chart in R?
- How to determine the order of bars in a matplotlib bar chart?
- How to plot a bar chart for a list in Python matplotlib?
- How to create a Matplotlib bar chart with a threshold line?
- How to create plotly bar chart with negative values in R?
- Create stacked bar chart with percentages on Y-axis using ggplot2 in R.
- How to sort bars in increasing order in a bar chart in matplotlib?
- How to have actual values in Matplotlib Pie Chart displayed?
- How to increase the thickness of error line in a Matplotlib bar chart?

Advertisements