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

Skip to content

Commit 91027f7

Browse files
committed
ENH: Subpanels
1 parent 61807e1 commit 91027f7

File tree

4 files changed

+1827
-1556
lines changed

4 files changed

+1827
-1556
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ def _make_layout_margins(fig, renderer, *, w_pad=0, h_pad=0,
207207
hspace=hspace, wspace=wspace)
208208
panel._layoutgrid.parent.edit_outer_margin_mins(margins, ss)
209209

210-
# for ax in [a for a in fig._localaxes if hasattr(a, 'get_subplotspec')]:
211-
for ax in fig.get_axes():
210+
for ax in fig._localaxes:
212211
if not hasattr(ax, 'get_subplotspec') or not ax.get_in_layout():
213212
continue
214213

@@ -223,7 +222,6 @@ def _make_layout_margins(fig, renderer, *, w_pad=0, h_pad=0,
223222
hspace=hspace, wspace=wspace)
224223
margin0 = margin.copy()
225224
pos, bbox = _get_pos_and_bbox(ax, renderer)
226-
227225
# the margin is the distance between the bounding box of the axes
228226
# and its position (plus the padding from above)
229227
margin['left'] += pos.x0 - bbox.x0
@@ -455,9 +453,7 @@ def _reposition_axes(fig, renderer, *, w_pad=0, h_pad=0, hspace=0, wspace=0):
455453
w_pad=w_pad, h_pad=h_pad,
456454
wspace=wspace, hspace=hspace)
457455

458-
# for ax in fig._localaxes:
459-
# if not hasattr(a, 'get_subplotspec'):
460-
for ax in fig.get_axes():
456+
for ax in fig._localaxes:
461457
if not hasattr(ax, 'get_subplotspec') or not ax.get_in_layout():
462458
continue
463459

lib/matplotlib/axes/_axes.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,31 @@
3939
_log = logging.getLogger(__name__)
4040

4141

42+
class _InsetLocator:
43+
"""
44+
Axes locator for `.Axes.inset_axes`.
45+
46+
The locater is a callable object used in `.Axes.set_aspect` to compute the
47+
axes location depending on the renderer.
48+
"""
49+
50+
def __init__(self, bounds, transform):
51+
"""
52+
*bounds* (a ``[l, b, w, h]`` rectangle) and *transform* together
53+
specify the position of the inset axes.
54+
"""
55+
self._bounds = bounds
56+
self._transform = transform
57+
58+
def __call__(self, ax, renderer):
59+
# Subtracting transPanel will typically rely on inverted(), freezing
60+
# the transform; thus, this needs to be delayed until draw time as
61+
# transPanel may otherwise change after this is evaluated.
62+
return mtransforms.TransformedBbox(
63+
mtransforms.Bbox.from_bounds(*self._bounds),
64+
self._transform - ax.figure.transPanel)
65+
66+
4267
# The axes module contains all the wrappers to plotting functions.
4368
# All the other methods should go in the _AxesBase class.
4469

@@ -449,7 +474,7 @@ def indicate_inset(self, bounds, inset_ax=None, *, transform=None,
449474

450475
# decide which two of the lines to keep visible....
451476
pos = inset_ax.get_position()
452-
bboxins = pos.transformed(self.figure.transFigure)
477+
bboxins = pos.transformed(self.figure.transPanel)
453478
rectbbox = mtransforms.Bbox.from_bounds(
454479
*bounds
455480
).transformed(transform)

lib/matplotlib/axes/_base.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ def set_figure(self, fig):
666666
super().set_figure(fig)
667667

668668
self.bbox = mtransforms.TransformedBbox(self._position,
669-
fig.transFigure)
669+
fig.transPanel)
670670
# these will be updated later as data is added
671671
self.dataLim = mtransforms.Bbox.null()
672672
self._viewLim = mtransforms.Bbox.unit()
@@ -1645,9 +1645,11 @@ def apply_aspect(self, position=None):
16451645
self._set_position(position, which='active')
16461646
return
16471647

1648-
fig_width, fig_height = self.get_figure().get_size_inches()
1649-
fig_aspect = fig_height / fig_width
1650-
1648+
trans = self.get_figure().transPanel
1649+
bb = mtransforms.Bbox.from_bounds(0, 0, 1, 1).transformed(trans)
1650+
# this is the physical aspect of the panel (or figure):
1651+
fig_aspect = bb.height / bb.width
1652+
16511653
if self._adjustable == 'box':
16521654
if self in self._twinned_axes:
16531655
raise RuntimeError("Adjustable 'box' is not allowed in a "

0 commit comments

Comments
 (0)