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/312662b47e0b66270daf97a55a12a07c60487a9be1fdaf95b16dc772a84a2d5e.png

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

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

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

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

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

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

Matplotlib 色の書式

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

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

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

表示範囲#

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

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

正規化#

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

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

累積値#

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

fig, ax = plt.subplots()
ax.hist(x, cumulative=True)
plt.show()
../_images/a6200dd7ded473347596b11599dec15a0528d62829ffcbb065b27c6e68934a27.png
fig, ax = plt.subplots()
ax.hist(x, cumulative=-1)
plt.show()
../_images/4199e602c4fbdef901426c7412349ec112d27c765341cdc0638dfdd0f1965c98.png

線のみのヒストグラム#

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

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

横向きのヒストグラム#

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

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

縦軸を対数にする#

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

fig, ax = plt.subplots()
ax.hist(x, log=True)
plt.show()
../_images/4793a39fc224bceaaa60998dba786150157ee48b0426339e5ef5718e14f96944.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/dbdce4d287a74c25c0120340ddf0b15342cef2381c4babd43563bde22481a536.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/812cd6c8142950f0b13b5dbbf9412f10c7bdd945ce61caf20201509278a0398a.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/8e1059bb2189b1d4d3f1cc7aa85e3d89268a5cabfa01ac185ee9434d9b008613.png