ヒストグラムを表示するPythonコード
データからヒストグラムをPythonで出力してみよう。
Pythonコード
matplotlib.pyplot
のhist()
関数を利用して, ヒストグラムを出力する。
hist()
の引数には, ヒストグラムにするデータを入力する。階級の個数bins
を指定する。
入力例. scores
のデータからヒストグラムを表示する。100点満点のうち10点ずつ区切るために, ヒストグラムの棒は10本にする。
import matplotlib.pyplot as plt
import japanize_matplotlib
# データ
scores = [87, 74, 79, 92, 88, 60, 79, 68, 68, 74, 71, 84,
77, 71, 74, 73, 84, 67, 73, 61, 44, 76, 78, 62,
92, 55, 70, 68, 85, 84, 71, 73, 61, 50, 66, 71,
82, 82, 66, 66, 59, 55, 52, 89, 64, 65, 57, 77,
53, 67]
# ヒストグラムの描画
plt.hist(scores, bins=10, color="skyblue", edgecolor="black")
plt.title("テスト点数の分布")
plt.xlabel("点数")
plt.ylabel("人数")
plt.grid(axis='y')
plt.show()

※matplotlib
で日本語を利用する場合japanize_matplotlib
を使う。必要であれば、次を行い事前にインストールしておく。
pip install japanize-matplotlib