Skip to content

Instantly share code, notes, and snippets.

@adrn
Created April 29, 2013 00:46
Show Gist options
  • Save adrn/5479070 to your computer and use it in GitHub Desktop.
Save adrn/5479070 to your computer and use it in GitHub Desktop.
Make a histogram with labeled bars
import numpy as np
import matplotlib.pyplot as plt
def label_bars(rects):
# attach some text labels to the bars
for rect in rects:
height = rect.get_height()
plt.text(rect.get_x()+rect.get_width()/2.,
height, str(int(height)),
ha='center', va='bottom')
data = np.random.normal(5., 1., size=1000)
n, bins = np.histogram(data, bins=25)
bin_centers = (bins[1:]+bins[:-1])/2.
width = bin_centers[1]-bin_centers[0]
bars = plt.bar(bin_centers, n, width,
color='g', edgecolor='none')
label_bars(bars)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment