mpl_gui.FigureRegistry.subplots#
- FigureRegistry.subplots(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 物件的一維 numpy 物件陣列。
對於 NxM,N>1 且 M>1 的子圖將以二維陣列形式返回。
如果為 False,則不執行任何擠壓:返回的 Axes 物件始終是包含 Axes 實例的二維陣列,即使它最終為 1x1。
- subplot_kwdict,選用
包含關鍵字的 Dict,這些關鍵字會傳遞給用於建立每個子圖的
add_subplot
呼叫。- gridspec_kwdict,選用
包含關鍵字的 Dict,這些關鍵字會傳遞給用於建立子圖所在網格的
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)