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

Skip to content

Commit 10ce68f

Browse files
committed
Made layoutboxes private
1 parent 228f15c commit 10ce68f

File tree

12 files changed

+165
-171
lines changed

12 files changed

+165
-171
lines changed

doc/users/next_whats_new/constrained_layout.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Constrained Layout Manager
1111
A new method to automatically decide spacing between subplots and their
1212
organizing ``GridSpec`` instances has been added. It is meant to
1313
replace the venerable ``tight_layout`` method. It is invoked via
14-
a new ``plt.figure(constrained_layout=True)`` kwarg to
14+
a new ``constrained_layout=True`` kwarg to
1515
`~.figure.Figure` or `~.figure.subplots`.
1616

1717
There are new ``rcParams`` for this package, and spacing can be
@@ -55,5 +55,5 @@ New ``figure`` kwarg for ``GridSpec``
5555
In order to facilitate ``constrained_layout``, ``GridSpec`` now accepts a
5656
``figure`` keyword. This is backwards compatible, in that not supplying this
5757
will simply cause ``constrained_layout`` to not operate on the subplots
58-
orgainzed by this ``GridSpec`` instance. Routines that use ``GridSpec`` (i.e.
59-
``ax.subplots``) have been modified to pass the figure to ``GridSpec``.
58+
orgainzed by this ``GridSpec`` instance. Routines that use ``GridSpec`` (e.g.
59+
``fig.subplots``) have been modified to pass the figure to ``GridSpec``.

lib/matplotlib/_constrained_layout.py

Lines changed: 74 additions & 74 deletions
Large diffs are not rendered by default.

lib/matplotlib/_layoutbox.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ def nonetree(lb):
659659
def nonechildren(lb):
660660
for child in lb.children:
661661
nonechildren(child)
662-
lb.artist.layoutbox = None
662+
lb.artist._layoutbox = None
663663
lb = None
664664

665665

lib/matplotlib/axes/_base.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -568,16 +568,16 @@ def __init__(self, fig, rect,
568568
right=rcParams['ytick.right'] and rcParams['ytick.major.right'],
569569
which='major')
570570

571-
self.layoutbox = None
572-
self.poslayoutbox = None
571+
self._layoutbox = None
572+
self._poslayoutbox = None
573573

574574
def __getstate__(self):
575575
# The renderer should be re-created by the figure, and then cached at
576576
# that point.
577577
state = super(_AxesBase, self).__getstate__()
578578
state['_cachedRenderer'] = None
579579
state.pop('layoutbox')
580-
state.pop('poslayoutbox')
580+
state.pop('_poslayoutbox')
581581

582582
return state
583583

@@ -590,8 +590,8 @@ def __setstate__(self, state):
590590
for artist in container:
591591
artist._remove_method = container.remove
592592
self._stale = True
593-
self.layoutbox = None
594-
self.poslayoutbox = None
593+
self._layoutbox = None
594+
self._poslayoutbox = None
595595

596596
def get_window_extent(self, *args, **kwargs):
597597
"""
@@ -889,10 +889,10 @@ def set_position(self, pos, which='both'):
889889
890890
"""
891891
self._set_position(pos, which='both')
892-
# because this is being called esternally to the library we
892+
# because this is being called externally to the library we
893893
# zero the constrained layout parts.
894-
self.layoutbox = None
895-
self.poslayoutbox = None
894+
self._layoutbox = None
895+
self._poslayoutbox = None
896896

897897
def _set_position(self, pos, which='both'):
898898
"""
@@ -2603,13 +2603,13 @@ def set_layoutbox(self, layoutbox):
26032603
"""
26042604
Set the layoutbox for this axis...
26052605
"""
2606-
self.layoutbox = layoutbox
2606+
self._layoutbox = layoutbox
26072607

26082608
def set_poslayoutbox(self, layoutbox):
26092609
"""
26102610
Set the spinelayoutbox for this axis...
26112611
"""
2612-
self.poslayoutbox = layoutbox
2612+
self._poslayoutbox = layoutbox
26132613

26142614
# Axes rectangle characteristics
26152615

@@ -4182,19 +4182,19 @@ def _make_twin_axes(self, *kl, **kwargs):
41824182
ax2 = self.figure.add_axes(self.get_position(True), *kl, **kwargs)
41834183
# check if we have a layoutbox. If so, then set this axis
41844184
# gets the same poslayoutbox and layoutbox...
4185-
if self.layoutbox is not None:
4186-
name = self.layoutbox.name + 'twin' + layoutbox.seq_id()
4187-
ax2.layoutbox = layoutbox.LayoutBox(
4188-
parent=self._subplotspec.layoutbox,
4185+
if self._layoutbox is not None:
4186+
name = self._layoutbox.name + 'twin' + layoutbox.seq_id()
4187+
ax2._layoutbox = layoutbox.LayoutBox(
4188+
parent=self._subplotspec._layoutbox,
41894189
name=name,
41904190
artist=ax2)
4191-
ax2.poslayoutbox = layoutbox.LayoutBox(
4192-
parent=ax2.layoutbox,
4193-
name=ax2.layoutbox.name+'.pos',
4191+
ax2._poslayoutbox = layoutbox.LayoutBox(
4192+
parent=ax2._layoutbox,
4193+
name=ax2._layoutbox.name+'.pos',
41944194
pos=True, subplot=True, artist=ax2)
41954195
# make the layout boxes be the same
4196-
ax2.layoutbox.constrain_same(self.layoutbox)
4197-
ax2.poslayoutbox.constrain_same(self.poslayoutbox)
4196+
ax2._layoutbox.constrain_same(self._layoutbox)
4197+
ax2._poslayoutbox.constrain_same(self._poslayoutbox)
41984198
return ax2
41994199

42004200
def twinx(self):

lib/matplotlib/axes/_subplots.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def __init__(self, fig, *args, **kwargs):
3232
being created. *plotNum* starts at 1 in the upper left
3333
corner and increases to the right.
3434
35-
3635
If *numRows* <= *numCols* <= *plotNum* < 10, *args* can be the
3736
decimal integer *numRows* * 100 + *numCols* * 10 + *plotNum*.
3837
"""
@@ -47,9 +46,8 @@ def __init__(self, fig, *args, **kwargs):
4746
s = str(int(args[0]))
4847
rows, cols, num = map(int, s)
4948
except ValueError:
50-
raise ValueError(
51-
'Single argument to subplot must be a 3-digit '
52-
'integer')
49+
raise ValueError('Single argument to subplot must be '
50+
'a 3-digit integer')
5351
self._subplotspec = GridSpec(rows, cols,
5452
figure=self.figure)[num - 1]
5553
# num - 1 for converting from MATLAB to python indexing
@@ -65,8 +63,7 @@ def __init__(self, fig, *args, **kwargs):
6563
else:
6664
if num < 1 or num > rows*cols:
6765
raise ValueError(
68-
(
69-
"num must be 1 <= num <= {maxn}, not {num}"
66+
("num must be 1 <= num <= {maxn}, not {num}"
7067
).format(maxn=rows*cols, num=num))
7168
self._subplotspec = GridSpec(
7269
rows, cols, figure=self.figure)[int(num) - 1]
@@ -81,19 +78,19 @@ def __init__(self, fig, *args, **kwargs):
8178
# add a layout box to this, for both the full axis, and the poss
8279
# of the axis. We need both because the axes may become smaller
8380
# due to parasitic axes and hence no longer fill the subplotspec.
84-
if self._subplotspec.layoutbox is None:
85-
self.layoutbox = None
86-
self.poslayoutbox = None
81+
if self._subplotspec._layoutbox is None:
82+
self._layoutbox = None
83+
self._poslayoutbox = None
8784
else:
88-
name = self._subplotspec.layoutbox.name + '.ax'
85+
name = self._subplotspec._layoutbox.name + '.ax'
8986
name = name + layoutbox.seq_id()
90-
self.layoutbox = layoutbox.LayoutBox(
91-
parent=self._subplotspec.layoutbox,
87+
self._layoutbox = layoutbox.LayoutBox(
88+
parent=self._subplotspec._layoutbox,
9289
name=name,
9390
artist=self)
94-
self.poslayoutbox = layoutbox.LayoutBox(
95-
parent=self.layoutbox,
96-
name=self.layoutbox.name+'.pos',
91+
self._poslayoutbox = layoutbox.LayoutBox(
92+
parent=self._layoutbox,
93+
name=self._layoutbox.name+'.pos',
9794
pos=True, subplot=True, artist=self)
9895

9996
def __reduce__(self):
@@ -118,10 +115,6 @@ def get_geometry(self):
118115
# COVERAGE NOTE: Never used internally or from examples
119116
def change_geometry(self, numrows, numcols, num):
120117
"""change subplot geometry, e.g., from 1,1,1 to 2,2,3"""
121-
if self.figure.get_constrained_layout():
122-
gridspecfig = fig
123-
else:
124-
gridspecfig = None
125118
self._subplotspec = GridSpec(numrows, numcols,
126119
figure=self.figure)[num - 1]
127120
self.update_params()

lib/matplotlib/colorbar.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
11351135

11361136
# check if using constrained_layout:
11371137
gs = parents[0].get_subplotspec().get_gridspec()
1138-
using_constrained_layout = (gs.layoutbox is not None)
1138+
using_constrained_layout = (gs._layoutbox is not None)
11391139
# defaults are not appropriate for constrained_layout:
11401140
pad0 = loc_settings['pad']
11411141
if using_constrained_layout:
@@ -1262,9 +1262,7 @@ def make_axes_gridspec(parent, **kw):
12621262
# constrained_layout can't remove and replace the tree
12631263
# hierarchy w/o a seg fault.
12641264
gs = parent.get_subplotspec().get_gridspec()
1265-
layoutbox.nonetree(gs.layoutbox)
1266-
#warnings.warn("colorbar called with use_gridspec=True. Disabling "
1267-
# "constrained_layout.")
1265+
layoutbox.nonetree(gs._layoutbox)
12681266
gs_from_subplotspec = gridspec.GridSpecFromSubplotSpec
12691267
if orientation == 'vertical':
12701268
pad = kw.pop('pad', 0.05)

lib/matplotlib/figure.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def __init__(self,
293293
subplotpars=None, # default to rc
294294
tight_layout=None, # default to rc figure.autolayout
295295
constrained_layout=None, # default to rc
296-
#figure.constrainedlayout
296+
#figure.constrained_layout.use
297297
):
298298
"""
299299
Parameters
@@ -334,7 +334,7 @@ def __init__(self,
334334
:doc:`/tutorials/intermediate/constrainedlayout_guide`
335335
for examples. (Note: does not work with :meth:`.subplot` or
336336
:meth:`.subplot2grid`.)
337-
Defaults to rc ``figure.constrainedlayout.do``.
337+
Defaults to rc ``figure.constrained_layout.use``.
338338
"""
339339
Artist.__init__(self)
340340
# remove the non-figure artist _axes property
@@ -387,7 +387,7 @@ def __init__(self,
387387

388388
self.subplotpars = subplotpars
389389
# constrained_layout:
390-
self.layoutbox = None
390+
self._layoutbox = None
391391
# set in set_constrained_layout_pads()
392392
self._constrained_layout_pads = dict()
393393
self._constrained_layout_pads['w_pad'] = None
@@ -529,7 +529,7 @@ def get_constrained_layout(self):
529529
def set_constrained_layout(self, constrained):
530530
"""
531531
Set whether ``constrained_layout`` is used upon drawing. If None,
532-
the rcParams['figure.constrainedlayout'] value will be set.
532+
the rcParams['figure.constrained_layout.use'] value will be used.
533533
534534
When providing a dict containing the keys `w_pad`, `h_pad`
535535
the default ``constrained_layout`` paddings will be
@@ -541,7 +541,7 @@ def set_constrained_layout(self, constrained):
541541
See :doc:`/tutorials/intermediate/constrainedlayout_guide`
542542
"""
543543
if constrained is None:
544-
constrained = rcParams['figure.constrainedlayout.do']
544+
constrained = rcParams['figure.constrained_layout.use']
545545
self._constrained = bool(constrained)
546546
if isinstance(constrained, dict):
547547
self.set_constrained_layout_pads(padd=constrained)
@@ -589,7 +589,7 @@ def set_constrained_layout_pads(self, **kwargs):
589589

590590
for td in todo:
591591
# default:
592-
pad = rcParams['figure.constrainedlayout.' + td]
592+
pad = rcParams['figure.constrained_layout.' + td]
593593
if padd is not None and td in padd:
594594
if padd[td] is not None:
595595
pad = padd[td]
@@ -754,18 +754,18 @@ def suptitle(self, t, **kwargs):
754754
sup.remove()
755755
else:
756756
self._suptitle = sup
757-
if self.layoutbox is not None:
757+
if self._layoutbox is not None:
758758
# assign a layout box to the suptitle...
759-
figlb = self.layoutbox
760-
self._suptitle.layoutbox = layoutbox.LayoutBox(
759+
figlb = self._layoutbox
760+
self._suptitle._layoutbox = layoutbox.LayoutBox(
761761
parent=figlb,
762762
name=figlb.name+'.suptitle')
763763
for child in figlb.children:
764-
if not (child == self._suptitle.layoutbox):
764+
if not (child == self._suptitle._layoutbox):
765765
w_pad, h_pad, wspace, hspace = \
766766
self.get_constrained_layout_pads(
767767
relative=True)
768-
layoutbox.vstack([self._suptitle.layoutbox, child],
768+
layoutbox.vstack([self._suptitle._layoutbox, child],
769769
padding=h_pad*2., strength='required')
770770
self.stale = True
771771
return self._suptitle
@@ -1891,7 +1891,7 @@ def __getstate__(self):
18911891
state.pop('layoutbox', None)
18921892
# suptitle:
18931893
if self._suptitle is not None:
1894-
self._suptitle.layoutbox = None
1894+
self._suptitle._layoutbox = None
18951895

18961896
return state
18971897

@@ -1910,7 +1910,7 @@ def __setstate__(self, state):
19101910
# re-initialise some of the unstored state information
19111911
self._axobservers = []
19121912
self.canvas = None
1913-
self.layoutbox = None
1913+
self._layoutbox = None
19141914

19151915
if restore_to_pylab:
19161916
# lazy import to avoid circularity
@@ -2220,11 +2220,11 @@ def init_layoutbox(self):
22202220
"""
22212221
initilaize the layoutbox for use in constrained_layout.
22222222
"""
2223-
if self.layoutbox is None:
2224-
self.layoutbox = layoutbox.LayoutBox(parent=None,
2223+
if self._layoutbox is None:
2224+
self._layoutbox = layoutbox.LayoutBox(parent=None,
22252225
name='figlb',
22262226
artist=self)
2227-
self.layoutbox.constrain_geometry(0., 0., 1., 1.)
2227+
self._layoutbox.constrain_geometry(0., 0., 1., 1.)
22282228

22292229
def execute_constrained_layout(self, renderer=None):
22302230
"""
@@ -2236,7 +2236,7 @@ def execute_constrained_layout(self, renderer=None):
22362236
from matplotlib._constrained_layout import (do_constrained_layout)
22372237

22382238
_log.debug('Executing constrainedlayout')
2239-
if self.layoutbox is None:
2239+
if self._layoutbox is None:
22402240
warnings.warn("Calling figure.constrained_layout, but figure "
22412241
"not setup to do constrained layout. "
22422242
" You either called GridSpec without the "

lib/matplotlib/gridspec.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ def __init__(self, nrows, ncols, figure=None,
208208
if (self.figure is None) or not self.figure.get_constrained_layout():
209209
_log.info("GridSpec must be called with the fig keyword if "
210210
"constrained_layout is used")
211-
self.layoutbox = None
211+
self._layoutbox = None
212212
else:
213213
self.figure.init_layoutbox()
214-
self.layoutbox = layoutbox.LayoutBox(
215-
parent=self.figure.layoutbox,
214+
self._layoutbox = layoutbox.LayoutBox(
215+
parent=self.figure._layoutbox,
216216
name='gridspec' + layoutbox.seq_id(),
217217
artist=self)
218218
# by default the layoutbox for a gridsepc will fill a figure.
@@ -232,7 +232,7 @@ def __getstate__(self):
232232
def __setstate__(self, state):
233233
self.__dict__ = state
234234
# layoutboxes don't survive pickling...
235-
self.layoutbox = None
235+
self._layoutbox = None
236236

237237
def update(self, **kwargs):
238238
"""
@@ -348,12 +348,12 @@ def __init__(self, nrows, ncols,
348348
width_ratios=width_ratios,
349349
height_ratios=height_ratios)
350350
# do the layoutboxes
351-
subspeclb = subplot_spec.layoutbox
351+
subspeclb = subplot_spec._layoutbox
352352
if subspeclb is None:
353-
self.layoutbox = None
353+
self._layoutbox = None
354354
else:
355355
# OK, this is needed to divide the figure.
356-
self.layoutbox = subspeclb.layout_from_subplotspec(
356+
self._layoutbox = subspeclb.layout_from_subplotspec(
357357
subplot_spec,
358358
name=subspeclb.name + '.gridspec' + layoutbox.seq_id(),
359359
artist=self)
@@ -401,18 +401,18 @@ def __init__(self, gridspec, num1, num2=None):
401401
self._gridspec = gridspec
402402
self.num1 = num1
403403
self.num2 = num2
404-
if gridspec.layoutbox is not None:
405-
glb = gridspec.layoutbox
404+
if gridspec._layoutbox is not None:
405+
glb = gridspec._layoutbox
406406
# So note that here we don't assign any layout yet,
407407
# just make the layoutbox that will conatin all items
408408
# associated w/ this axis. This can include other axes like
409409
# a colorbar or a legend.
410-
self.layoutbox = layoutbox.LayoutBox(
410+
self._layoutbox = layoutbox.LayoutBox(
411411
parent=glb,
412412
name=glb.name + '.ss' + layoutbox.seq_id(),
413413
artist=self)
414414
else:
415-
self.layoutbox = None
415+
self._layoutbox = None
416416

417417
def __getstate__(self):
418418
state = self.__dict__
@@ -425,7 +425,7 @@ def __getstate__(self):
425425
def __setstate__(self, state):
426426
self.__dict__ = state
427427
# layoutboxes don't survive pickling...
428-
self.layoutbox = None
428+
self._layoutbox = None
429429

430430
def get_gridspec(self):
431431
return self._gridspec

0 commit comments

Comments
 (0)