

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- How to add a legend on Seaborn facetgrid bar plot using Matplotlib?
- How to turn off error bars in Seaborn 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 to add a title on Seaborn lmplot?
- How to create plotly bar chart with values on top of bars in R?
- How to plot multiple histograms on same plot with Seaborn using Matplotlib?
- How can I add textures to my bars and wedges in Matplotlib?
- How to change Bar Chart values to percentages in Matplotlib?
- How to add row percentages to contingency table in R?
- How can box plot be overlaid on top of swarm plot in Seaborn?
- How to show tick labels on top of a matplotlib plot?
- How can Tensorflow be used to add dense layers on top using Python?
- How to change the figuresize using Seaborn factorplot in Matplotlib?
Advertisements