LoginSignup
1
1

More than 5 years have passed since last update.

Pythonでヒストグラムをさくっと描画

Last updated at Posted at 2018-03-17

グラフの描画にはmatplotlibモジュールを使用します。

histgram.py
import numpy as np
import matplotlib.pyplot as plt

#np.random.normal(平均, 標準偏差, 正規乱数の個数)
x = np.random.normal(50, 10, 1000)

plt.hist(x, bins=50)
plt.title("Histgram")
plt.xlabel("x")
plt.ylabel("y")
plt.show()

hist関数でbins引数では、ヒストグラムで表示される棒の数を指定できます。デフォルトでは10が自動的に指定されます。

他の表示テクニック

範囲を指定する
plt.hist(x, range=(50, 100))

正規化
plt.hist(x, normed=True)

累積値を出力
plt.hist(x, cumulative=True)

ヒストグラムの下に余白を入れる
plt.hist(x, bottom=30)

縦軸を対数で表示
plt.hist(x, log=True)

棒の幅を設定
plt.hist(x, rwidth=0.8)

棒の色を設定(デフォルトだと青です)
plt.hist(x, color="red")

ぬりつぶしなし
plt.hist(x, histtype="step")

ぬりつぶしあり
plt.hist(x, histtype="stepfilled")

各棒の位置を目盛りの左側に設定
plt.hist(x, align="left")

今度は右側に設定
plt.hist(x, align="right")

縦軸と横軸を入れ替えて表示
plt.hist(x, orientation="horizontal")

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1