需要的庫:matplotlib
用法:
import matplotlib.pyplot as plt plt.plot(x, y) #x, y是兩個列表 plt.show()
例子:
import matplotlib.pyplot as plt x = [1, 2, 3] y = [2, 3, 4] plt.plot(x, y) #x, y是兩個列表
plt.show()
效果如下:

實際上,x, y的點越多,圖像越精確,我們可以用numpy庫生成方便的x,y
當點數量不足的時候,圖像可能大相徑庭
比如
import matplotlib.pyplot as plt
import numpy as np import math
x = np.linspace(-100, 100, 20) #分別代表最小,最大,數量, 生成一個等差數列 y = [math.sin(t) for t in x] plt.plot(x, y) plt.show()
本意是輸出 y = sin(x) 的圖像, 實際上的結果是:
當我們把點數量增加到1000
import matplotlib.pyplot as plt
import numpy as np import math
x = np.linspace(-10, 10, 1000) y = [math.sin(t) for t in x] plt.plot(x, y) plt.show()
效果:
我們可以改變y的取值生成各種函數圖像。