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

Skip to content

Commit 536dcae

Browse files
committed
Fixes after review
1 parent 7d84999 commit 536dcae

File tree

4 files changed

+51
-47
lines changed

4 files changed

+51
-47
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
219219
ax.set_frame_on(False)
220220
ax.set_xticks([])
221221
ax.set_yticks([])
222-
ax.set_facecolor((1., 0., 0., 0.))
222+
ax.set_facecolor((1, 0, 0, 0))
223223

224224
# for each axes, make a margin between the *pos* layoutbox and the
225225
# *axes* layoutbox be a minimum size that can accomodate the
@@ -252,15 +252,15 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
252252
# or width. This stops that... It *should* have been
253253
# taken into account w/ pref_width...
254254
if fig._layoutbox.constrained_layout_called < 1:
255-
ax._poslayoutbox.constrain_height_min(20., strength='weak')
256-
ax._poslayoutbox.constrain_width_min(20., strength='weak')
257-
ax._layoutbox.constrain_height_min(20., strength='weak')
258-
ax._layoutbox.constrain_width_min(20., strength='weak')
259-
ax._poslayoutbox.constrain_top_margin(0., strength='weak')
260-
ax._poslayoutbox.constrain_bottom_margin(0.,
255+
ax._poslayoutbox.constrain_height_min(20, strength='weak')
256+
ax._poslayoutbox.constrain_width_min(20, strength='weak')
257+
ax._layoutbox.constrain_height_min(20, strength='weak')
258+
ax._layoutbox.constrain_width_min(20, strength='weak')
259+
ax._poslayoutbox.constrain_top_margin(0, strength='weak')
260+
ax._poslayoutbox.constrain_bottom_margin(0,
261261
strength='weak')
262-
ax._poslayoutbox.constrain_right_margin(0., strength='weak')
263-
ax._poslayoutbox.constrain_left_margin(0., strength='weak')
262+
ax._poslayoutbox.constrain_right_margin(0, strength='weak')
263+
ax._poslayoutbox.constrain_left_margin(0, strength='weak')
264264

265265
# do layout for suptitle.
266266
if fig._suptitle is not None:
@@ -285,8 +285,7 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
285285
# parent gridspec. The ones that contain axes are not so constrained.
286286
figlb = fig._layoutbox
287287
for child in figlb.children:
288-
name = (child.name).split('.')[-1][:-3]
289-
if name == 'gridspec':
288+
if child._is_gridspec_layoutbox():
290289
# farm the gridspec layout out.
291290
#
292291
# This routine makes all the subplot spec containers
@@ -437,8 +436,7 @@ def arange_subplotspecs(gs, hspace=0, wspace=0):
437436
"""
438437
sschildren = []
439438
for child in gs.children:
440-
name = (child.name).split('.')[-1][:-3]
441-
if name == 'ss':
439+
if child._is_subplotspec_layoutbox():
442440
for child2 in child.children:
443441
# check for gridspec children...
444442
name = (child2.name).split('.')[-1][:-3]
@@ -513,10 +511,10 @@ def layoutcolorbarsingle(ax, cax, shrink, aspect, location, pad=0.05):
513511
else:
514512
layoutbox.hstack([lb, axlb], padding=pad * axlb.width)
515513
# constrain the height and center...
516-
layoutbox.match_heights([axpos, lbpos], [1., shrink])
514+
layoutbox.match_heights([axpos, lbpos], [1, shrink])
517515
layoutbox.align([axpos, lbpos], 'v_center')
518516
# set the width of the pos box
519-
lbpos.constrain_width(shrink * axpos.height * (1./aspect),
517+
lbpos.constrain_width(shrink * axpos.height * (1/aspect),
520518
strength='strong')
521519
elif location in ('bottom', 'top'):
522520
lbpos = layoutbox.LayoutBox(
@@ -533,7 +531,7 @@ def layoutcolorbarsingle(ax, cax, shrink, aspect, location, pad=0.05):
533531
layoutbox.vstack([lb, axlb], padding=pad * axlb.height)
534532
# constrain the height and center...
535533
layoutbox.match_widths([axpos, lbpos],
536-
[1., shrink], strength='strong')
534+
[1, shrink], strength='strong')
537535
layoutbox.align([axpos, lbpos], 'h_center')
538536
# set the height of the pos box
539537
lbpos.constrain_height(axpos.width * aspect * shrink,
@@ -605,7 +603,7 @@ def layoutcolorbargridspec(parents, cax, shrink, aspect, location, pad=0.05):
605603
shrink, strength='strong')
606604
lbpos.constrain_bottom(
607605
(maxposlb.top - minposlb.bottom) *
608-
(1. - shrink)/2. + minposlb.bottom,
606+
(1 - shrink)/2 + minposlb.bottom,
609607
strength='strong')
610608

611609
# set the width of the pos box
@@ -647,7 +645,7 @@ def layoutcolorbargridspec(parents, cax, shrink, aspect, location, pad=0.05):
647645
shrink)
648646
lbpos.constrain_left(
649647
(maxposlb.right - minposlb.left) *
650-
(1.-shrink)/2. + minposlb.left)
648+
(1-shrink)/2 + minposlb.left)
651649
# set the height of the pos box
652650
lbpos.constrain_height(lbpos.width * shrink * aspect,
653651
strength='medium')

lib/matplotlib/_layoutbox.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,26 @@ def constrain_top(self, top, strength='strong'):
353353
c = (self.top == top)
354354
self.solver.addConstraint(c | strength)
355355

356+
def _is_subplotspec_layoutbox(self):
357+
'''
358+
Helper to check if this layoutbox is the layoutbox of a
359+
subplotspec
360+
'''
361+
name = (self.name).split('.')[-1][:-3]
362+
if name == 'ss':
363+
return True
364+
return False
365+
366+
def _is_gridspec_layoutbox(self):
367+
'''
368+
Helper to check if this layoutbox is the layoutbox of a
369+
gridspec
370+
'''
371+
name = (self.name).split('.')[-1][:-3]
372+
if name == 'gridspec':
373+
return True
374+
return False
375+
356376
def find_child_subplots(self):
357377
'''
358378
Find children of this layout box that are subplots. We want to line

lib/matplotlib/figure.py

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,6 @@ def __init__(self,
389389
# constrained_layout:
390390
self._layoutbox = None
391391
# set in set_constrained_layout_pads()
392-
self._constrained_layout_pads = dict()
393-
self._constrained_layout_pads['w_pad'] = None
394-
self._constrained_layout_pads['h_pad'] = None
395-
self._constrained_layout_pads['wspace'] = None
396-
self._constrained_layout_pads['hspace'] = None
397392
self.set_constrained_layout(constrained_layout)
398393

399394
self.set_tight_layout(tight_layout)
@@ -540,19 +535,25 @@ def set_constrained_layout(self, constrained):
540535
541536
See :doc:`/tutorials/intermediate/constrainedlayout_guide`
542537
"""
538+
self._constrained_layout_pads = dict()
539+
self._constrained_layout_pads['w_pad'] = None
540+
self._constrained_layout_pads['h_pad'] = None
541+
self._constrained_layout_pads['wspace'] = None
542+
self._constrained_layout_pads['hspace'] = None
543543
if constrained is None:
544544
constrained = rcParams['figure.constrained_layout.use']
545545
self._constrained = bool(constrained)
546546
if isinstance(constrained, dict):
547-
self.set_constrained_layout_pads(padd=constrained)
547+
self.set_constrained_layout_pads(**constrained)
548548
else:
549549
self.set_constrained_layout_pads()
550550

551551
self.stale = True
552552

553553
def set_constrained_layout_pads(self, **kwargs):
554554
"""
555-
Set padding for ``constrained_layout``.
555+
Set padding for ``constrained_layout``. Note the kwargs can be passed
556+
as a dictionary ``fig.set_constrained_layout(**paddict)``.
556557
557558
Parameters:
558559
-----------
@@ -573,31 +574,16 @@ def set_constrained_layout_pads(self, **kwargs):
573574
Height padding between subplots, expressed as a fraction of the
574575
subplot width. The total padding ends up being h_pad + hspace.
575576
576-
padd : dict
577-
Dictionary with possible keywords as above. If a keyword
578-
is specified then it takes precedence over the dictionary.
579-
580577
See :doc:`/tutorials/intermediate/constrainedlayout_guide`
581578
"""
582579

583580
todo = ['w_pad', 'h_pad', 'wspace', 'hspace']
584-
585-
if 'padd' in kwargs:
586-
padd = kwargs['padd']
587-
else:
588-
padd = None
589-
590581
for td in todo:
591-
# default:
592-
pad = rcParams['figure.constrained_layout.' + td]
593-
if padd is not None and td in padd:
594-
if padd[td] is not None:
595-
pad = padd[td]
596-
if td in kwargs:
597-
# kw arg override padd or rcParam
598-
if kwargs[td] is not None:
599-
pad = kwargs[td]
600-
self._constrained_layout_pads[td] = pad
582+
if td in kwargs and kwargs[td] is not None:
583+
self._constrained_layout_pads[td] = kwargs[td]
584+
else:
585+
self._constrained_layout_pads[td] = (
586+
rcParams['figure.constrained_layout.' + td])
601587

602588
def get_constrained_layout_pads(self, relative=False):
603589
"""

lib/matplotlib/gridspec.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def __getstate__(self):
225225
state = self.__dict__
226226
try:
227227
state.pop('_layoutbox')
228-
except:
228+
except KeyError:
229229
pass
230230
return state
231231

@@ -418,7 +418,7 @@ def __getstate__(self):
418418
state = self.__dict__
419419
try:
420420
state.pop('_layoutbox')
421-
except:
421+
except KeyError:
422422
pass
423423
return state
424424

0 commit comments

Comments
 (0)