如何在函數中傳遞matplotlib 繪圖對象

往往我們可能希望創建一個繪圖對象,然後對其進行修改,或者作爲函數參數。

那麼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)

結果如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章