matplotlib 基礎
利用 matplotlib 進行簡單繪圖
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-np.pi, np.pi, 256, endpoint = True)
C, S = np.cos(x), np.sin(x)
# 設定線條樣式
plt.plot(x, C, color = "red", linewidth = 2.5, linestyle = '-')
plt.plot(x, S, color = "blue", linewidth = 2.5, linestyle = '-')
plt.show()
matplotlib.pyplot.plot 函式常用。
# 設定橫軸、縱軸的界限
plt.xlim(x.min() * 1.1, x.max() * 1.1)
npi = np.pi
plt.xticks([-npi, -npi / 2, 0, npi / 2, npi], [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$']) # 值和標籤
plt.ylim(C.min() * 1.1, C.max() * 1.1)
plt.yticks([-1, 0, 1], [r'$-1$', r'$0$', r'$1$'])