Skip to content

Commit 2f6147f

Browse files
committed
Track twinned axes and keep their positions synchronized
1 parent f407192 commit 2f6147f

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

lib/matplotlib/axes/_base.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ class _AxesBase(martist.Artist):
414414

415415
_shared_x_axes = cbook.Grouper()
416416
_shared_y_axes = cbook.Grouper()
417+
_twinned_axes = cbook.Grouper()
417418

418419
def __str__(self):
419420
return "{0}({1[0]:g},{1[1]:g};{1[2]:g}x{1[3]:g})".format(
@@ -874,10 +875,14 @@ def set_position(self, pos, which='both'):
874875
"""
875876
if not isinstance(pos, mtransforms.BboxBase):
876877
pos = mtransforms.Bbox.from_bounds(*pos)
877-
if which in ('both', 'active'):
878-
self._position.set(pos)
879-
if which in ('both', 'original'):
880-
self._originalPosition.set(pos)
878+
twins = self._twinned_axes.get_siblings(self)
879+
if not twins:
880+
twins = [self]
881+
for ax in twins:
882+
if which in ('both', 'active'):
883+
ax._position.set(pos)
884+
if which in ('both', 'original'):
885+
ax._originalPosition.set(pos)
881886
self.stale = True
882887

883888
def reset_position(self):
@@ -887,8 +892,12 @@ def reset_position(self):
887892
This resets the a possible position change due to aspect constraints.
888893
For an explanation of the positions see `.set_position`.
889894
"""
890-
pos = self.get_position(original=True)
891-
self.set_position(pos, which='active')
895+
twins = self._twinned_axes.get_siblings(self)
896+
if not twins:
897+
twins = [self]
898+
for ax in twins:
899+
pos = ax.get_position(original=True)
900+
ax.set_position(pos, which='active')
892901

893902
def set_axes_locator(self, locator):
894903
"""
@@ -4116,10 +4125,12 @@ def _make_twin_axes(self, *kl, **kwargs):
41164125
"""
41174126
make a twinx axes of self. This is used for twinx and twiny.
41184127
"""
4128+
# Typically, SubplotBase._make_twin_axes is called instead of this.
41194129
ax2 = self.figure.add_axes(self.get_position(True), *kl, **kwargs)
41204130
# do not touch every thing shared, just this and it's twin.
41214131
self.set_adjustable('datalim')
41224132
ax2.set_adjustable('datalim')
4133+
self._twinned_axes.join(self, ax2)
41234134
return ax2
41244135

41254136
def twinx(self):

lib/matplotlib/axes/_subplots.py

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def _make_twin_axes(self, *kl, **kwargs):
154154
ax2 = subplot_class_factory(projection_class)(self.figure,
155155
*kl, **kwargs)
156156
self.figure.add_subplot(ax2)
157+
self._twinned_axes.join(self, ax2)
157158
return ax2
158159

159160
_subplot_classes = {}

lib/mpl_toolkits/axes_grid1/axes_divider.py

+1
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,7 @@ def _make_twin_axes(self, *kl, **kwargs):
918918
ax2 = type(self)(self.figure, self.get_position(True), *kl, **kwargs)
919919
ax2.set_axes_locator(self.get_axes_locator())
920920
self.figure.add_axes(ax2)
921+
self._twinned_axes.join(self, ax2)
921922
return ax2
922923

923924
_locatableaxes_classes = {}

0 commit comments

Comments
 (0)