1. 程式人生 > >python中使用matpltlib畫圖

python中使用matpltlib畫圖

import numpy as np
import matplotlib.pyplot as plt

#先定義幾組資料
x = [0,1,2,3,4,5,6,7,8,9]
y1 = np.sin(x)
y2 = np.cos(x)
#畫圖
plt.plot(x, y1, linestyle="--", color="red")
plt.plot(x, y2, linestyle = "-", color= "blue")
#定義橫縱軸
plt.xlabel("x")
plt.ylabel("sin & cos")
#定義legend
plt.legend(["sin","cos"],loc = "upper right")
plt.show()