@@ -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:
@@ -199,15 +220,22 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
199
220
200
221
fig ._layoutbox .constrained_layout_called += 1
201
222
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' )
223
+ # check if any axes collapsed to zero. If not, don't change positions:
224
+
225
+ if _axes_all_finite_sized (fig ):
226
+ # Now set the position of the axes...
227
+ for ax in fig .axes :
228
+ if ax ._layoutbox is not None :
229
+ newpos = ax ._poslayoutbox .get_rect ()
230
+
231
+ # Now set the new position.
232
+ # ax.set_position will zero out the layout for
233
+ # this axis, allowing users to hard-code the position,
234
+ # so this does the same w/o zeroing layout.
235
+ ax ._set_position (newpos , which = 'original' )
236
+ else :
237
+ warnings .warn ('constrained_layout not applied. At least '
238
+ 'one axes collapsed to zero width or height.' )
211
239
212
240
213
241
def _make_ghost_gridspec_slots (fig , gs ):
0 commit comments