Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 362c34c

Browse files
committed
FIX: CL avoid fully collapsed axes
FIX: turn constrained_layout off for ZOOM and PAN
1 parent dfb8936 commit 362c34c

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ def _in_same_row(rownum0min, rownum0max, rownumCmin, rownumCmax):
6666
or rownumCmin <= rownum0max <= rownumCmax)
6767

6868

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+
6982
######################################################
7083
def do_constrained_layout(fig, renderer, h_pad, w_pad,
7184
hspace=None, wspace=None):
@@ -132,6 +145,14 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
132145
133146
'''
134147

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+
135156
invTransFig = fig.transFigure.inverted().transform_bbox
136157

137158
# list of unique gridspecs that contain child axes:
@@ -199,15 +220,22 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
199220

200221
fig._layoutbox.constrained_layout_called += 1
201222
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.')
211239

212240

213241
def _make_ghost_gridspec_slots(fig, gs):

0 commit comments

Comments
 (0)