@@ -194,8 +194,8 @@ def add_offsetboxes(ax, size=10, margin=.1, color='black'):
194194 """
195195 m , mp = margin , 1 + margin
196196 anchor_points = [(- m , - m ), (- m , .5 ), (- m , mp ),
197- (mp , .5 ), (.5 , mp ), (mp , mp ),
198- (.5 , - m ), ( mp , - m ), (.5 , - m )]
197+ (.5 , mp ), (mp , mp ), (mp , .5 ),
198+ (mp , - m ), (.5 , - m )]
199199 for point in anchor_points :
200200 da = DrawingArea (size , size )
201201 background = Rectangle ((0 , 0 ), width = size ,
@@ -215,48 +215,78 @@ def add_offsetboxes(ax, size=10, margin=.1, color='black'):
215215 bbox_transform = ax .transAxes ,
216216 borderpad = 0. )
217217 ax .add_artist (anchored_box )
218- return anchored_box
219218
220219
221- @image_comparison (['tight_layout_offsetboxes1' , 'tight_layout_offsetboxes2' ],
222- style = 'mpl20' )
223220def test_tight_layout_offsetboxes ():
224- # 1 .
221+ # 0 .
225222 # - Create 4 subplots
226223 # - Plot a diagonal line on them
224+ # - Use tight_layout
225+ #
226+ # 1.
227+ # - Same 4 subplots
227228 # - Surround each plot with 7 boxes
228229 # - Use tight_layout
229- # - See that the squares are included in the tight_layout
230- # and that the squares in the middle do not overlap
230+ # - See that the squares are included in the tight_layout and that the squares do
231+ # not overlap
231232 #
232233 # 2.
233- # - Make the squares around the right side axes invisible
234- # - See that the invisible squares do not affect the
235- # tight_layout
234+ # - Make the squares around the Axes invisible
235+ # - See that the invisible squares do not affect the tight_layout
236236 rows = cols = 2
237237 colors = ['red' , 'blue' , 'green' , 'yellow' ]
238238 x = y = [0 , 1 ]
239239
240- def _subplots ():
241- _ , axs = plt .subplots (rows , cols )
242- axs = axs .flat
243- for ax , color in zip (axs , colors ):
240+ def _subplots (with_boxes ):
241+ fig , axs = plt .subplots (rows , cols )
242+ for ax , color in zip (axs .flat , colors ):
244243 ax .plot (x , y , color = color )
245- add_offsetboxes (ax , 20 , color = color )
246- return axs
244+ if with_boxes :
245+ add_offsetboxes (ax , 20 , color = color )
246+ return fig , axs
247+
248+ # 0.
249+ fig0 , axs0 = _subplots (False )
250+ fig0 .tight_layout ()
247251
248252 # 1.
249- axs = _subplots ()
250- plt .tight_layout ()
253+ fig1 , axs1 = _subplots (True )
254+ fig1 .tight_layout ()
255+
256+ # The AnchoredOffsetbox should be added to the bounding of the Axes, causing them to
257+ # be smaller than the plain figure.
258+ for ax0 , ax1 in zip (axs0 .flat , axs1 .flat ):
259+ bbox0 = ax0 .get_position ()
260+ bbox1 = ax1 .get_position ()
261+ assert bbox1 .x0 > bbox0 .x0
262+ assert bbox1 .x1 < bbox0 .x1
263+ assert bbox1 .y0 > bbox0 .y0
264+ assert bbox1 .y1 < bbox0 .y1
265+
266+ # No AnchoredOffsetbox should overlap with another.
267+ bboxes = []
268+ for ax1 in axs1 .flat :
269+ for child in ax1 .get_children ():
270+ if not isinstance (child , AnchoredOffsetbox ):
271+ continue
272+ bbox = child .get_window_extent ()
273+ for other_bbox in bboxes :
274+ assert not bbox .overlaps (other_bbox )
275+ bboxes .append (bbox )
251276
252277 # 2.
253- axs = _subplots ()
254- for ax in ( axs [ cols - 1 :: rows ]) :
278+ fig2 , axs2 = _subplots (True )
279+ for ax in axs2 . flat :
255280 for child in ax .get_children ():
256281 if isinstance (child , AnchoredOffsetbox ):
257282 child .set_visible (False )
258-
259- plt .tight_layout ()
283+ fig2 .tight_layout ()
284+ # The invisible AnchoredOffsetbox should not count for tight layout, so it should
285+ # look the same as when they were never added.
286+ for ax0 , ax2 in zip (axs0 .flat , axs2 .flat ):
287+ bbox0 = ax0 .get_position ()
288+ bbox2 = ax2 .get_position ()
289+ assert_array_equal (bbox2 .get_points (), bbox0 .get_points ())
260290
261291
262292def test_empty_layout ():
0 commit comments