シンプルな折れ線グラフを表示するサンプルプログラムをあげておきます。
ちなみにシンプルな折れ線グラフは以下のグラフです。

pyplotとオブジェクト指向(object-oriented)の2つのパターンのコードをあげます。
pyplot(plt)
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0,5,0.1)
y = x*2
# グラフの生成
plt.plot(x, y, label="y=2x")
plt.xlabel("x")
plt.ylabel("y")
plt.title("sample graph")
plt.legend()
plt.savefig("sample_graph.jpg")
plt.show()
object-oriented(ax, fig)
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0,5,0.1)
y = x*2
# グラフの生成
fig, ax = plt.subplots()
ax.plot(x,y,label="y=2x")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_title("sample graph")
ax.legend()
plt.savefig("sample_graph.jpg")
plt.show()
参考文献
https://matplotlib.org/tutorials/introductory/pyplot.html#sphx-glr-tutorials-introductory-pyplot-py

コメント