8000 Merge pull request #11627 from jklymak/fix-bad-title-CL-interaction · matplotlib/matplotlib@0545020 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0545020

Browse files
authored
Merge pull request #11627 from jklymak/fix-bad-title-CL-interaction
FIX: CL avoid fully collapsed axes
2 parents c1d086f + 6d8c9c1 commit 0545020

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 36 additions & 11 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:
@@ -187,8 +208,6 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
187208
figlb = fig._layoutbox
188209
for child in figlb.children:
189210
if child._is_gridspec_layoutbox():
190-
# farm the gridspec layout out.
191-
#
192211
# This routine makes all the subplot spec containers
193212
# have the correct arrangement. It just stacks the
194213
# subplot layoutboxes in the correct order...
@@ -199,15 +218,21 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
199218

200219
fig._layoutbox.constrained_layout_called += 1
201220
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.')
211236

212237

213238
def _make_ghost_gridspec_slots(fig, gs):

0 commit comments

Comments
 (0)
0