Skip to content

Instantly share code, notes, and snippets.

@brianckeegan
Last active August 29, 2015 14:00
Show Gist options
  • Save brianckeegan/11098328 to your computer and use it in GitHub Desktop.
Save brianckeegan/11098328 to your computer and use it in GitHub Desktop.
Put labels inside barcharts.
# adapted from http://matplotlib.org/examples/api/barchart_demo.html
def autolabel(rects):
max_height = max([rect.get_height() for rect in rects if hasattr(rect,'get_height') and not np.isnan(rect.get_height())])
for rect in rects:
if hasattr(rect,'get_height'):
height = rect.get_height()
if not np.isnan(height):
ax.text(rect.get_x()+rect.get_width()/2., height-.1*max_height, '%d'%int(height),
ha='center', va='bottom',color='w')
@brianckeegan
Copy link
Author

Use case for plotting a barchart using a Pandas DataFrame called df:

ax = df.plot(kind='bar')
autolabel(ax.patches)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment