- 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
How to add percentages on top of bars in Seaborn using Matplotlib?
To add percentages on top of bars in Seaborn, we can take the following steps −
Create the lists, x, y and percentages to plot using Seaborn.
Using barplot, show point estimates and confidence intervals with bars. Store the returned axis.
Find patches from the returned axis (In step 2).
Iterate the patches (returned in step 3).
Find x and y from the patches to place the percentage value at the top of the bars.
To display the figure, use show() method.
Example
import matplotlib.pyplot as plt import seaborn as sns plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = ['A', 'B', 'C', 'D', 'E'] y = [1, 3, 2, 0, 4] percentage = [10, 30, 20, 0, 40] ax = sns.barplot(x=x, y=y, palette='PuBuGn_r') patches = ax.patches for i in range(len(patches)): x = patches[i].get_x() + patches[i].get_width()/2 y = patches[i].get_height()+.05 ax.annotate('{:.1f}%'.format(percentage[i]), (x, y), ha='center') plt.show()
Output
- Related Articles
- How to turn off error bars in Seaborn Bar Plot using Matplotlib?
- How to add a legend on Seaborn facetgrid bar plot using Matplotlib?
- Plotting error bars from a dataframe using Seaborn FacetGrid (Matplotlib)
- Add minor gridlines to Matplotlib plot using Seaborn
- How to create a bar plot in R with label of bars on top of the bars using ggplot2?
- How can I add textures to my bars and wedges in Matplotlib?
- How to plot multiple histograms on same plot with Seaborn using Matplotlib?
- How to add a title on Seaborn lmplot?
- How to change the figuresize using Seaborn factorplot in Matplotlib?
- How to plot multiple Seaborn Jointplot in Subplot using Matplotlib?
- How to adjust transparency (alpha) in Seaborn pairplot using Matplotlib?
- How to change Bar Chart values to percentages in Matplotlib?
- How to create plotly bar chart with values on top of bars in R?
- How to add row percentages to contingency table in R?
- How to make a heatmap square in Seaborn FacetGrid using Matplotlib?

Advertisements