Created
April 29, 2013 00:46
-
-
Save adrn/5479070 to your computer and use it in GitHub Desktop.
Make a histogram with labeled bars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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