mpl_gui.subplots#
- mpl_gui.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)[原始碼]#
建立一個圖形和一組子圖。
這個實用程式包裝器可以方便地在單次呼叫中建立子圖的常用佈局,包括封閉的圖形物件。
- 參數:
- nrows, ncolsint,預設值:1
子圖網格的列/欄數。
- sharex, shareybool 或 {'none', 'all', 'row', 'col'},預設值:False
控制 x (sharex) 或 y (sharey) 軸之間的屬性共享
True 或 'all':x 軸或 y 軸將在所有子圖之間共享。
False 或 'none':每個子圖的 x 軸或 y 軸將是獨立的。
'row':每個子圖列將共享一個 x 軸或 y 軸。
'col':每個子圖欄將共享一個 x 軸或 y 軸。
當子圖沿著一欄共享 x 軸時,只會建立底部子圖的 x 刻度標籤。同樣地,當子圖沿著一列共享 y 軸時,只會建立第一欄子圖的 y 刻度標籤。若要稍後開啟其他子圖的刻度標籤,請使用
tick_params
。當子圖具有帶單位的共享軸時,呼叫
set_units
將會更新每個軸的新單位。- squeezebool,預設值:True
如果為 True,則會從返回的
Axes
陣列中擠出多餘的維度如果只建構一個子圖 (nrows=ncols=1),則會將產生的單一 Axes 物件作為純量傳回。
對於 Nx1 或 1xM 子圖,傳回的物件是 Axes 物件的 1D numpy 物件陣列。
對於 NxM,具有 N>1 和 M>1 的子圖會以 2D 陣列傳回。
如果為 False,則完全不會進行擠壓:傳回的 Axes 物件永遠是包含 Axes 執行個體的 2D 陣列,即使它最終變成 1x1。
- subplot_kwdict,選用
包含傳遞至用於建立每個子圖的
add_subplot
呼叫的關鍵字的字典。- gridspec_kwdict,選用
包含傳遞至用於建立子圖放置網格的
GridSpec
建構函式的關鍵字的字典。- **fig_kw
所有其他關鍵字引數都會傳遞至
figure
呼叫。
- 傳回:
- fig
Figure
- ax
Axes
或 Axes 陣列 如果建立超過一個子圖,則 ax 可以是單一
Axes
物件或 Axes 物件的陣列。可以使用 squeeze 關鍵字控制產生的陣列的維度,請參閱上方。處理傳回值的典型慣用語如下
# using the variable ax for single a Axes fig, ax = plt.subplots() # using the variable axs for multiple Axes fig, axs = plt.subplots(2, 2) # using tuple unpacking for multiple Axes fig, (ax1, ax2) = plt.subplots(1, 2) fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
與
axes
相比,建議使用名稱ax
和複數形式的axs
,因為後者無法明確指出它是指單一Axes
執行個體還是這些執行個體的集合。
- fig
範例
# First create some toy data: x = np.linspace(0, 2*np.pi, 400) y = np.sin(x**2) # Create just a figure and only one subplot fig, ax = plt.subplots() ax.plot(x, y) ax.set_title('Simple plot') # Create two subplots and unpack the output array immediately f, (ax1, ax2) = plt.subplots(1, 2, sharey=True) ax1.plot(x, y) ax1.set_title('Sharing Y axis') ax2.scatter(x, y) # Create four polar axes and access them through the returned array fig, axs = plt.subplots(2, 2, subplot_kw=dict(projection="polar")) axs[0, 0].plot(x, y) axs[1, 1].scatter(x, y) # Share a X axis with each column of subplots plt.subplots(2, 2, sharex='col') # Share a Y axis with each row of subplots plt.subplots(2, 2, sharey='row') # Share both X and Y axes with all subplots plt.subplots(2, 2, sharex='all', sharey='all') # Note that this is the same as plt.subplots(2, 2, sharex=True, sharey=True) # Create figure number 10 with a single subplot # and clears it if it already exists. fig, ax = plt.subplots(num=10, clear=True)