diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index c299fb3b0f94..05438a6c909c 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1061,10 +1061,11 @@ def set_position(self, pos, which='both'): matplotlib.transforms.Bbox.from_bounds matplotlib.transforms.Bbox.from_extents """ - self._set_position(pos, which=which) - # because this is being called externally to the library we - # don't let it be in the layout. - self.set_in_layout(False) + for ax in self._twinned_axes.get_siblings(self): + ax._set_position(pos, which=which) + # because this is being called externally to the library we + # don't let it be in the layout. + ax.set_in_layout(False) def _set_position(self, pos, which='both'): """ diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 9552b7f9e47c..0019559860e3 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -399,6 +399,18 @@ def test_twinx_axis_scales(): ax2.margins(0, 0) +@pytest.mark.parametrize('twin', ('x', 'y')) +def test_twin_moved(twin): + fig, ax = plt.subplots() + # test twinx or twiny + ax_twin = getattr(ax, f'twin{twin}')() + ax.set_position([0.2, 0.2, 0.5, 0.5]) + fig.draw_without_rendering() + + assert_array_equal(ax.bbox.extents, + ax_twin.bbox.extents) + + def test_twin_inherit_autoscale_setting(): fig, ax = plt.subplots() ax_x_on = ax.twinx()