@@ -2084,6 +2084,216 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None,
20842084 pad = pad , h_pad = h_pad , w_pad = w_pad , rect = rect )
20852085 self .subplots_adjust (** kwargs )
20862086
2087+ def align_xlabels (self , axs = None , renderer = None ):
2088+ """
2089+ Align the xlabels of subplots in this figure.
2090+
2091+ If a label is on the bottom, it is aligned with labels on axes that
2092+ also have their label on the bottom and that have the same
2093+ bottom-most subplot row. If the label is on the top,
2094+ it is aligned with labels on axes with the same top-most row.
2095+
2096+ Parameters
2097+ ----------
2098+ axs : list of `~matplotlib.axes.Axes` (None)
2099+ Optional list of `~matplotlib.axes.Axes` to align
2100+ the xlabels.
2101+
2102+ renderer : (None)
2103+ Optional renderer to do the adjustment on.
2104+
2105+ See Also
2106+ --------
2107+ matplotlib.figure.Figure.align_ylabels
2108+
2109+ matplotlib.figure.Figure.align_labels
2110+
2111+ Example
2112+ -------
2113+ Example with rotated xtick labels::
2114+
2115+ fig, axs = plt.subplots(1, 2)
2116+ for tick in axs[0].get_xticklabels():
2117+ tick.set_rotation(55)
2118+ axs[0].set_xlabel('XLabel 0')
2119+ axs[1].set_xlabel('XLabel 1')
2120+ fig.align_xlabels()
2121+
2122+ """
2123+
2124+ from .tight_layout import get_renderer
2125+
2126+ if renderer is None :
2127+ renderer = get_renderer (self )
2128+
2129+ if axs is None :
2130+ axs = self .axes
2131+
2132+ axs = np .asarray (np .array (axs )).flatten ().tolist ()
2133+
2134+ for ax in axs :
2135+ _log .debug (' Working on: %s' , ax .get_xlabel ())
2136+ ss = ax .get_subplotspec ()
2137+ nrows , ncols , row0 , row1 , col0 , col1 = ss .get_rows_columns ()
2138+ same = [ax ]
2139+ labpo = ax .xaxis .get_label_position ()
2140+ for axc in axs :
2141+ if axc .xaxis .get_label_position () == labpo :
2142+ ss = axc .get_subplotspec ()
2143+ nrows , ncols , rowc0 , rowc1 , colc , col1 = \
2144+ ss .get_rows_columns ()
2145+ if (labpo == 'bottom' ) and (rowc1 == row1 ):
2146+ same += [axc ]
2147+ elif (labpo == 'top' ) and (rowc0 == row0 ):
2148+ same += [axc ]
2149+
2150+ for axx in same :
2151+ _log .debug (' Same: %s' , axx .xaxis .label )
2152+ axx .xaxis ._align_label_siblings += [ax .xaxis ]
2153+
2154+ def align_ylabels (self , axs = None , renderer = None ):
2155+ """
2156+ Align the ylabels of subplots in this figure.
2157+
2158+ If a label is on the left, it is aligned with labels on axes that
2159+ also have their label on the left and that have the same
2160+ left-most subplot column. If the label is on the right,
2161+ it is aligned with labels on axes with the same right-most column.
2162+
2163+ Parameters
2164+ ----------
2165+ axs : list of `~matplotlib.axes.Axes` (None)
2166+ Optional list of `~matplotlib.axes.Axes` to align
2167+ the ylabels.
2168+
2169+ renderer : (None)
2170+ Optional renderer to do the adjustment on.
2171+
2172+ See Also
2173+ --------
2174+ matplotlib.figure.Figure.align_xlabels
2175+
2176+ matplotlib.figure.Figure.align_labels
2177+
2178+ Example
2179+ -------
2180+ Example with large yticks labels::
2181+
2182+ fig, axs = plt.subplots(2, 1)
2183+ axs[0].plot(np.arange(0, 1000, 50))
2184+ axs[0].set_ylabel('YLabel 0')
2185+ axs[1].set_ylabel('YLabel 1')
2186+ fig.align_ylabels()
2187+
2188+ """
2189+
2190+ from .tight_layout import get_renderer
2191+
2192+ if renderer is None :
2193+ renderer = get_renderer (self )
2194+
2195+ if axs is None :
2196+ axs = self .axes
2197+
2198+ axs = np .asarray (np .array (axs )).flatten ().tolist ()
2199+ for ax in axs :
2200+ _log .debug (' Working on: %s' , ax .get_ylabel ())
2201+ ss = ax .get_subplotspec ()
2202+ nrows , ncols , row0 , row1 , col0 , col1 = ss .get_rows_columns ()
2203+ same = [ax ]
2204+ labpo = ax .yaxis .get_label_position ()
2205+ for axc in axs :
2206+ if axc != ax :
2207+ if axc .yaxis .get_label_position () == labpo :
2208+ ss = axc .get_subplotspec ()
2209+ nrows , ncols , row0 , row1 , colc0 , colc1 = \
2210+ ss .get_rows_columns ()
2211+ if (labpo == 'left' ) and (colc0 == col0 ):
2212+ same += [axc ]
2213+ elif (labpo == 'right' ) and (colc1 == col1 ):
2214+ same += [axc ]
2215+ for axx in same :
2216+ _log .debug (' Same: %s' , axx .yaxis .label )
2217+ axx .yaxis ._align_label_siblings += [ax .yaxis ]
2218+
2219+ # place holder until #9498 is merged...
2220+ def align_titles (self , axs = None , renderer = None ):
2221+ """
2222+ Align the titles of subplots in this figure.
2223+
2224+ Parameters
2225+ ----------
2226+ axs : list of `~matplotlib.axes.Axes` (None)
2227+ Optional list of axes to align the xlabels.
2228+
2229+ renderer : (None)
2230+ Optional renderer to do the adjustment on.
2231+
2232+ See Also
2233+ --------
2234+ matplotlib.figure.Figure.align_xlabels
2235+
2236+ matplotlib.figure.Figure.align_ylabels
2237+ """
2238+
2239+ from .tight_layout import get_renderer
2240+
2241+ if renderer is None :
2242+ renderer = get_renderer (self )
2243+
2244+ if axs is None :
2245+ axs = self .axes
2246+
2247+ while len (axs ):
2248+ ax = axs .pop ()
2249+ ax ._update_title_position (renderer )
2250+ same = [ax ]
2251+ if ax ._autolabelpos :
2252+ ss = ax .get_subplotspec ()
2253+ nrows , ncols , row0 , row1 , col0 , col1 = ss .get_rows_columns ()
2254+ labpo = ax .xaxis .get_label_position ()
2255+ for axc in axs :
2256+ axc ._update_title_position (renderer )
2257+ if axc ._autolabelpos :
2258+ ss = axc .get_subplotspec ()
2259+ nrows , ncols , rowc0 , rowc1 , colc , col1 = \
2260+ ss .get_rows_columns ()
2261+ if (rowc0 == row0 ):
2262+ same += [axc ]
2263+
2264+ x0 , y0 = ax .title .get_position ()
2265+ for axx in same :
2266+ x , y = axx .title .get_position ()
2267+ if y > y0 :
2268+ ax .title .set_position (x0 , y )
2269+ y0 = y
2270+ elif y0 > y :
2271+ axx .title .set_positions (x , y0 )
2272+
2273+ def align_labels (self , axs = None , renderer = None ):
2274+ """
2275+ Align the xlabels and ylabels of subplots with the same subplots
2276+ row or column (respectively).
2277+
2278+ Parameters
2279+ ----------
2280+ axs : list of `~matplotlib.axes.Axes` (None)
2281+ Optional list (or ndarray) of `~matplotlib.axes.Axes` to
2282+ align the labels.
2283+
2284+ renderer : (None)
2285+ Optional renderer to do the adjustment on.
2286+
2287+ See Also
2288+ --------
2289+ matplotlib.figure.Figure.align_xlabels
2290+
2291+ matplotlib.figure.Figure.align_ylabels
2292+ """
2293+ self .align_xlabels (axs = axs , renderer = renderer )
2294+ self .align_ylabels (axs = axs , renderer = renderer )
2295+ # self.align_titles(axs=axs, renderer=renderer)
2296+
20872297
20882298def figaspect (arg ):
20892299 """
0 commit comments