Histogram in matplotlib


In [1]:
%load_ext watermark
%watermark -a 'Hideki Tanaka' -u -d -v -p matplotlib,numpy,scipy


Hideki Tanaka 
last updated: 2016-02-23 

CPython 3.5.1
IPython 4.1.1

matplotlib 1.5.1
numpy 1.10.4
scipy 0.17.0

In [2]:
%matplotlib inline

In [3]:
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import numpy as np
from scipy.stats import norm

In [4]:
plt.style.use('ggplot')
plt.rc('xtick.major', size=0)
plt.rc('ytick.major', size=0)

In [5]:
np.random.seed(0)

In [6]:
x = np.random.randn(10000)
mu, sigma = norm.fit(x)

In [7]:
fig, ax = plt.subplots()
n, bins, patches = ax.hist(x, bins=50, normed=1, color="#3F5D7D")
y = mlab.normpdf(bins, mu, sigma)
l = plt.plot(bins, y, 'r--')
ax.set_title(r'$\mu=%.2f,\ \sigma=%.2f$' % (mu, sigma), fontsize=14)
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$\mathrm{Probability}$')
plt.show()



In [8]:
fig.savefig('../images/mpl_histogram.png', dpi=80)