ぱたへね

はてなダイアリーはrustの色分けができないのでこっちに来た

ヒストグラムを書く

pyplotでヒストグラムを書く。
元データは、例題で学ぶ統計的方法 6ページから。

ソース

# Python 3
from pylab import *
import numpy
import scipy
import math
prop = matplotlib.font_manager.FontProperties(fname=r'c:\windows\fonts\msgothic.ttc')

#ファイルからデータを読み込み、floatのlistに変換
data = list(map((lambda x: float(x[:-1])), open("data.txt", "r").readlines() ))
bins = 8

figure()

hist, binedges, patches = plt.hist(data, bins)
left = np.array(binedges[:-1])
right = np.array(binedges[1:])
bottom = np.zeros(len(left))
top = bottom + hist

# update the view limits
xlim(left[0]-1.0, right[-1]+1.0)
ylim(bottom.min(), top.max()+1.0)

xlabel('距離(km)', fontproperties = prop)
ylabel('度数', fontproperties = prop)

savefig('hist.png')
show()