Skip to content

Instantly share code, notes, and snippets.

@korkridake
Created November 23, 2018 09:43
Show Gist options
  • Save korkridake/4eb4214248f05cef2d829791def8d424 to your computer and use it in GitHub Desktop.
Save korkridake/4eb4214248f05cef2d829791def8d424 to your computer and use it in GitHub Desktop.
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# Seaborn Bar Chart with Annotations
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
import matplotlib.pyplot as plt
import seaborn as sns;
sns.set(style="whitegrid", color_codes=True)
%matplotlib inline
plt.figure(figsize=(10,8))
ax = sns.barplot(x="[INSERT YOUR X_Variable]", y="[INSERT YOUR Y_Variable]", color = '#512e5f', data=[INSERT YOUR DATA])
totals = [] # create a list to collect the plt.patches data
for i in ax.patches:
totals.append(i.get_height()) # find the values and append to list
total = sum(totals) # set individual bar lables using above list
# set individual bar lables using above list
for i in ax.patches:
# -------------------------------------------------------------------------
# get_x pulls left or right; get_height pushes up or down
# -------------------------------------------------------------------------
ax.text(i.get_x()+.08, i.get_height()+.00005, str(round((i.get_height()/total)*100, 2))+'%', fontsize=12, color='black')
plt.xlabel('[INSERT YOUR X_LABEL]')
plt.ylabel('[INSERT YOUR Y_LABEL]')
plt.title('[INSERT YOUR TITLE]')
plt.tight_layout()
plt.show();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment