Matplotlibでヒストグラムと累積グラフ

Matplotlibを使って, ヒストグラムと累積グラフを出力する簡単なプログラムをメモ. シンプルなものが欲しかったので.

import matplotlib.pylab as pylab
import numpy

data = numpy.random.normal(0, 1, 10000) # generating an input

fig = pylab.figure()

ax1 = fig.add_subplot(111)
n, bins, patches = ax1.hist(data, bins=100) # histogram
ax1.set_xlim(data.min(), data.max())

ax2 = ax1.twinx() # another y-axis
n, bins, patches = ax2.hist(
    data, bins=100, cumulative=True, normed=True, histtype='step', ec='k') # cumulative
ax2.set_xlim(data.min(), data.max())
ax2.set_ylim(0, 1)

pylab.show()

累積を出すときの"ec='k'"は線色を黒に指定している部分です. fcでバーの色も変更できます.

下記を参考にしました.

http://d.hatena.ne.jp/lolloo-htn/20111230/1325246359
http://d.hatena.ne.jp/white_wheels/20100327/p6