@@ -66,6 +66,19 @@ def _in_same_row(rownum0min, rownum0max, rownumCmin, rownumCmax):
66
66
or rownumCmin <= rownum0max <= rownumCmax )
67
67
68
68
69
+ def _axes_all_finite_sized (fig ):
70
+ """
71
+ helper function to make sure all axes in the
72
+ figure have a finite width and height. If not, return False
73
+ """
74
+ for ax in fig .axes :
75
+ if ax ._layoutbox is not None :
76
+ newpos = ax ._poslayoutbox .get_rect ()
77
+ if newpos [2 ] <= 0 or newpos [3 ] <= 0 :
78
+ return False
79
+ return True
80
+
81
+
69
82
######################################################
70
83
def do_constrained_layout (fig , renderer , h_pad , w_pad ,
71
84
hspace = None , wspace = None ):
@@ -132,6 +145,14 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
132
145
133
146
'''
134
147
148
+ try :
149
+ if fig .canvas .toolbar ._active in ('PAN' , 'ZOOM' ):
150
+ # don't do constrained layout during zoom and pan.
151
+ return
152
+ except AttributeError :
153
+ # not toolbar, or no _active attribute..
154
+ pass
155
+
135
156
invTransFig = fig .transFigure .inverted ().transform_bbox
136
157
137
158
# list of unique gridspecs that contain child axes:
@@ -187,8 +208,6 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
187
208
figlb = fig ._layoutbox
188
209
for child in figlb .children :
189
210
if child ._is_gridspec_layoutbox ():
190
- # farm the gridspec layout out.
191
- #
192
211
# This routine makes all the subplot spec containers
193
212
# have the correct arrangement. It just stacks the
194
213
# subplot layoutboxes in the correct order...
@@ -199,15 +218,21 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
199
218
200
219
fig ._layoutbox .constrained_layout_called += 1
201
220
fig ._layoutbox .update_variables ()
202
- # Now set the position of the axes...
203
- for ax in fig .axes :
204
- if ax ._layoutbox is not None :
205
- newpos = ax ._poslayoutbox .get_rect ()
206
- # Now set the new position.
207
- # ax.set_position will zero out the layout for
208
- # this axis, allowing users to hard-code the position,
209
- # so this does the same w/o zeroing layout.
210
- ax ._set_position (newpos , which = 'original' )
221
+
222
+ # check if any axes collapsed to zero. If not, don't change positions:
223
+ if _axes_all_finite_sized (fig ):
224
+ # Now set the position of the axes...
225
+ for ax in fig .axes :
226
+
8D4C
if ax ._layoutbox is not None :
227
+ newpos = ax ._poslayoutbox .get_rect ()
228
+ # Now set the new position.
229
+ # ax.set_position will zero out the layout for
230
+ # this axis, allowing users to hard-code the position,
231
+ # so this does the same w/o zeroing layout.
232
+ ax ._set_position (newpos , which = 'original' )
233
+ else :
234
+ warnings .warn ('constrained_layout not applied. At least '
235
+ 'one axes collapsed to zero width or height.' )
211
236
212
237
213
238
def _make_ghost_gridspec_slots (fig , gs ):
0 commit comments