散布図を表示するPythonコード
2変量のデータから散布図をPythonで出力してみよう。
Pythonコード
matplotlib.pyplot
のscatter()
関数を利用して, 散布図を出力する。
scatter()
の引数には, 散布図にする2つのデータを入力する。
入力例. height
とweight
のデータから散布図を表示する。
import matplotlib.pyplot as plt
import japanize_matplotlib
# データ
height = [150, 152, 155, 157, 158, 160, 161, 162, 163, 164,
165, 166, 167, 168, 169, 170, 171, 172, 173, 174,
175, 176, 177, 178, 179, 180, 181, 182, 183, 185]
weight = [45, 47, 48, 50, 51, 53, 54, 55, 55, 56,
57, 58, 59, 60, 61, 62, 63, 64, 64, 65,
66, 67, 68, 69, 70, 71, 72, 73, 74, 75]
# 散布図の描画
plt.scatter(height, weight, color="skyblue")
plt.title("身長と体重の関係")
plt.xlabel("身長 (cm)")
plt.ylabel("体重 (kg)")
plt.grid(True)
plt.show()

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