往往我們可能希望創建一個繪圖對象,然後對其進行修改,或者作爲函數參數。
那麼plt.plot 無法滿足, 而採用Axes 對象,可以創建和傳遞繪圖對象。
Axes 對象是subplot 底下的,如果我們只需要一張圖,那麼可以在subplot 中設置圖片數爲 1,1 。
例子:
def _profile(ax, x, y):
ln, = ax.plot(x, y)
# return the Artist created
return ln
def profile_matrix(n, m):
fig, ax_array = plt.subplots(n, m, sharex=True, sharey=True)
for ax in np.ravel(ax_array):
_profile(ax, np.arange(50), np.random.rand(50))
profile_matrix(1, 1)
結果如下: