折れ線グラフを表示するPythonコード
データから折れ線グラフをPythonで出力してみよう。
Pythonコード
matplotlib.pyplot
のplot()
関数を利用して, 折れ線グラフを出力する。
plot()
の引数には, 横軸と縦軸方向のデータを入力する。
入力例. months
とsales
のデータの折れ線グラフを表示する。
import matplotlib.pyplot as plt
import japanize_matplotlib
# データ
months = ["1月", "2月", "3月", "4月", "5月", "6月",
"7月", "8月", "9月", "10月", "11月", "12月"]
sales = [120, 150, 130, 170, 160, 180, 200, 190, 210, 230, 220, 250]
# 折れ線グラフ描画
plt.plot(months, sales, marker="o", color="skyblue", label="売上")
plt.title("月別売上")
plt.xlabel("月")
plt.ylabel("売上(万円)")
plt.grid(True)
plt.legend()
plt.show()

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