Matplotlibのヒストグラム#

公開日

Matplotlibでヒストグラムを出力するには、ax.histを使用します。ax.histの最初の引数に表示するデータを配列で与えます。

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(seed=0)
x = np.random.normal(10, 5, 100) # 平均10, 標準偏差5の正規分布で100点のデータを生成

fig, ax = plt.subplots()
ax.hist(x)
plt.show()
../_images/770db662d73ca568300551334415c3831dd7b0429d182ac1eec32ab89bcfa137.png

表示する棒の数はbinsオプションで指定できます(デフォルトは10)。

fig, ax = plt.subplots()
ax.hist(x, bins=5)
plt.show()
../_images/7be990608a5ded524cdb3c9bb719b97441e955973075407ba818de02f427f89f.png

棒の太さ・色を変更する#

棒の太さはrwidthオプションで指定できます。0から1の範囲を取り、値が小さいほど細くなります。

fig, ax = plt.subplots()
ax.hist(x, rwidth=0.2)
plt.show()
../_images/778420d42f35b6dbfe1e847fd0c869e4fe5a03d919dc3c6786ee31aefb0a1ae6.png

棒の色はcolorオプションで指定できます。colorオプションの詳細は以下の記事を参考にして下さい。

Matplotlib 色の書式

fig, ax = plt.subplots()
ax.hist(x, color="green")
plt.show()
../_images/b6a4d92abd497a99349748878e671c4cdffacc6fa28554247d28636a07d5f37a.png

また、alphaオプションで透明度を変更できます。0から1の範囲を取り、値が小さいほど透明に近づきます。

fig, ax = plt.subplots()
ax.hist(x, color="green", alpha=0.5)
plt.show()
../_images/472bd3b5bfc7f6226fa0a86ad6fc0ecc71a12fe66311e679e85b60768018f887.png

表示範囲#

表示範囲はrangeオプションで指定できます。最小値と最大値の順にタプルで与えます。rangeの外側にあるデータは無視されます。

fig, ax = plt.subplots()
ax.hist(x, range=(5, 15))
plt.show()
../_images/8c8dbe04d7a2bea22acb94d304d9de82760b6f22718a3bb7531efc52119b1a6c.png

正規化#

densityオプションをTrueにすると、合計面積(棒の横幅×縦の長さの合計)が1になるように正規化されます。

fig, ax = plt.subplots()
ax.hist(x, density=True)
plt.show()
../_images/f851534849e18bad25a600260cb3dd91635710ffc10ef22d79e7c6eddb15c917.png

累積値#

ヒストグラムを小さい値からの累積値で表示する場合、cumulativeオプションをTrueに指定します。反対に、大きい値からの累積値で表示する場合、cumulative-1に指定します。

fig, ax = plt.subplots()
ax.hist(x, cumulative=True)
plt.show()
../_images/c6d28c71584bf95ab539712aba78d78324254b732209377f4cb9c7673430c2c4.png
fig, ax = plt.subplots()
ax.hist(x, cumulative=-1)
plt.show()
../_images/becaff8007fda0eee34cbebdce4e0c3483c752566a03dbab3530ba371ea7aca9.png

線のみのヒストグラム#

histtypeオプションを"step"にすることで、塗り潰しをしない線のみのヒストグラムになります。

fig, ax = plt.subplots()
ax.hist(x, histtype="step")
plt.show()
../_images/d57e2d909ac68106d0fd3fc98c7c2c0a94dce3be6b09c2a1e07fee8dcebea3d8.png

横向きのヒストグラム#

orientationオプションをTrueにすることで、ヒストグラムが横向きになります。

fig, ax = plt.subplots()
ax.hist(x, orientation="horizontal")
plt.show()
../_images/6841b8e58d9bb7af92991b41d3734d5c3dbf161b2f49f4aa49f23d7ae16aedcd.png

縦軸を対数にする#

logオプションをTrueとすることで、縦軸を対数表示できます。

fig, ax = plt.subplots()
ax.hist(x, log=True)
plt.show()
../_images/7d2ee74e8d36bc19c951ffc065563128e9a5fdf669348771352eb3a9f0d58d21.png

複数系列のヒストグラム#

複数系列のデータからヒストグラムを作成する方法を示します。

横に並べる#

まず、単純に棒を横に並べる例です。ax.histに複数のデータをリストで渡します。

np.random.seed(seed=0)
x1 = np.random.normal(10, 5, 100) # 平均10, 標準偏差5の正規分布で100点のデータを生成
x2 = np.random.normal(20, 10, 100) # 平均20, 標準偏差10の正規分布で100点のデータを生成

fig, ax = plt.subplots()
ax.hist([x1, x2])
ax.legend(["x1", "x2"])
plt.show()
../_images/2ab15246e3c762c0f90c3abe2fd2048285fdb29e57b709979150d3b81a9f2ab6.png

透過させて重ねる#

alphaオプションを使って棒を透過させ、重ねて表示することも可能です。

fig, ax = plt.subplots()
ax.hist(x1, alpha=0.5, label="x1")
ax.hist(x2, alpha=0.5, label="x2")
ax.legend()
plt.show()
../_images/fb2167356db919c8503248c3f09d927efeff2fe1cb4fd88f31fc0eda059fe55a.png

積み上げる#

ヒストグラムを積み上げる場合、histtype="barstacked"とするか、stacked=Trueとします。

fig, ax = plt.subplots()
ax.hist([x1, x2], histtype="barstacked")
# ax.hist([x1, x2], stacked=True) # これも同じ結果になる
ax.legend(["x1", "x2"])
plt.show()
../_images/d98618722c003ec0945c37052178016aed9e0666cf41e96bbadcd6cce9f79240.png