matplotlib でヒストグラムを描く

本ページでは、Python のグラフ作成パッケージ Matplotlib を用いてヒストグラム (Histogram) を描く方法について紹介します。

matplotlib.pyplot.hist の概要

matplotlib には、ヒストグラムを描画するメソッドとして、matplotlib.pyplot.hist が用意されてます。

matplotlib.pyplot.histの使い方

matplotlib.pyplot.hist(x, bins=10, range=None, normed=False, weights=None,
                       cumulative=False, bottom=None, histtype='bar',
                       align='mid', orientation='vertical', rwidth=None,
                       log=False, color=None, label=None, stacked=False,
                       hold=None, data=None, **kwargs)

matplotlib.pyplot.hist の主要な引数

x (必須) ヒストグラムを作成するための生データの配列。
bins ビン (表示する棒) の数。階級数。(デフォルト値: 10)
range ビンの最小値と最大値を指定。(デフォルト値: (x.min(), x.max()))
normed True に設定すると正規化 (合計値が 1 になるように変換) を実施。 (デフォルト値: False)
cumulative True に設定すると、累積ヒストグラムを出力。 (デフォルト値: False)
bottom 各棒の下側の余白を数値または配列で指定。
histtype ‘bar’ (通常のヒストグラム), ‘barstacked’ (積み上げヒストグラム), ‘step’ (線), ‘stepfilled ‘ (塗りつぶしありの線) から選択。 (デフォルト値: ‘bar’)
align 各棒の中心を X 軸目盛上のどの横位置で出力するか。 ‘left’, ‘mid’, ‘right’ から選択。(デフォルト値: ‘mid’)
orientation 棒の方向。’horizontal’ (水平方向), ‘vertical’ (垂直方向) から選択。(デフォルト値: ‘vertical’)
rwidth 各棒の幅を数値または、配列で指定。
log True に設定すると、縦軸を対数目盛で表示します。
color ヒストグラムの色。配列で指定し、データセット単位で色を指定することができます。
label 凡例を載せる際に使用します。
stacked True に設定すると積み上げヒストグラムで出力します。False に設定すると、横に並べて出力します。

グラフの出力例

import numpy as np
import matplotlib.pyplot as plt

# 平均 50, 標準偏差 10 の正規乱数を1,000件生成
x = np.random.normal(50, 10, 1000)

# ヒストグラムを出力
plt.hist(x)

hist1

棒の数を 16 に指定

plt.hist(x, bins=16)

hist2

下限、上限を設定

plt.hist(x, range=(50, 100))

hist3

正規化 (合計すると 1 になるよう調整)

plt.hist(x, normed=True)

hist4

累積値を出力

plt.hist(x, cumulative=True)

hist5

ヒストグラム下側の余白を 30 に設定

plt.hist(x, bottom=30)

hist6

縦軸を対数目盛で表示

plt.hist(x, log=True)

hist13

棒の幅を 0.8 に設定

plt.hist(x, rwidth=0.8)

hist14

棒の色を設定

plt.hist(x, color="red")

hist15

塗りつぶしなしの線で出力

plt.hist(x, histtype="step")

hist8

塗りつぶしありの線で出力

plt.hist(x, histtype="stepfilled")

hist9


棒の位置・方向に関する設定

各棒の位置を目盛上の左側に設定

plt.hist(x, align="left")

hist10

各棒の位置を目盛上の右側に出力

plt.hist(x, align="right")

hist11

棒の向きを横方向に出力

plt.hist(x, orientation="horizontal")

hist12


積み上げヒストグラム

積み上げヒストグラム (histtype オプションで指定)

# 平均 20, 標準偏差 10 の正規乱数を1,000件生成
y = np.random.normal(10, 10, 1000)
plt.hist([x, y], histtype="barstacked")

hist7

積み上げヒストグラムとして出力 (stacked オプションで指定)

plt.hist([x, y], stacked=True)

hist17

積み上げヒストグラムの色を指定する

plt.hist([x, y], stacked=True, color=['#f46d43', '#66bd63'])

hist19

積み上げ棒グラフとして出力しない(複数の棒を並べて描画)

plt.hist([x, y], stacked=False)

hist18

凡例を表示

labels = ['V1', 'V2']
plt.hist([x, y], label=labels)
plt.legend()

hist16

参考: pyplot — Matplotlib 1.5.1 documentation