Matplotlib入門 – 折れ線グラフを作成する方法

基本

基本の折れ線グラフです。

import matplotlib.pyplot as plt
x_list = [1, 2, 3, 4, 5]
y_list = [67, 85, 31, 73, 47]
plt.plot(x_list, y_list)
plt.show()

複数

線が複数ある場合です。

import matplotlib.pyplot as plt
x_list = [1, 2, 3, 4, 5]
y0_list = [67, 85, 55, 73, 47]
y1_list = [53, 35, 79, 45, 63]
y2_list = [41, 45, 66, 52, 41]
plt.plot(x_list, y0_list)
plt.plot(x_list, y1_list)
plt.plot(x_list, y2_list)
plt.show()

タイトル、凡例、軸

タイトル、凡例、軸を表示する方法です。

import matplotlib.pyplot as plt

temp_list0 = [
    10.9, 10.8, 10.6, 10.7, 10.1, 10.5, 10.8, 10.6, 10.6, 10.3, 11.8, 10.3, 10.0, 11.9, 11.9, 11.2, 10.6, 10.6, 11.4, 11.9, 10.8, 11.2,
]
temp_list1 = [
    13.0, 12.7, 11.2, 11.1, 11.5, 11.2, 12.1, 11.8, 12.3, 12.1, 12.1, 12.4, 11.3, 11.3, 11.0, 12.1, 12.5, 11.6, 13.0, 12.8, 11.6, 11.6,
]
plt.plot(temp_list0, label="City0")
plt.plot(temp_list1, label="City1")
plt.xlabel("Year")
plt.ylabel("Temperature")
plt.title("Temperature")
plt.legend()
plt.xticks([0, 10, 20], [2002, 2012, 2022])
plt.show()

コメント

タイトルとURLをコピーしました