diff --git a/lib/matplotlib/axes/_secondary_axes.py b/lib/matplotlib/axes/_secondary_axes.py index 3cbe8e0f1ff1..b5b4598439c6 100644 --- a/lib/matplotlib/axes/_secondary_axes.py +++ b/lib/matplotlib/axes/_secondary_axes.py @@ -43,11 +43,12 @@ def _make_secondary_locator(rect, parent): *parent*. """ _rect = mtransforms.Bbox.from_bounds(*rect) - bb = mtransforms.TransformedBbox(_rect, parent.transAxes) - tr = parent.figure.transFigure.inverted() - bb = mtransforms.TransformedBbox(bb, tr) - def secondary_locator(ax, renderer): + # delay evaluating transform until draw time because the + # parent transform may have changed (i.e. if window reesized) + bb = mtransforms.TransformedBbox(_rect, parent.transAxes) + tr = parent.figure.transFigure.inverted() + bb = mtransforms.TransformedBbox(bb, tr) return bb return secondary_locator diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 1a848692907e..7ef48bbd2ee2 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -6106,3 +6106,16 @@ def test_secondary_fail(): axsec = ax.secondary_xaxis('right') with pytest.raises(ValueError): axsec = ax.secondary_yaxis('bottom') + + +def test_secondary_resize(): + fig, ax = plt.subplots(figsize=(10, 5)) + ax.plot(np.arange(2, 11), np.arange(2, 11)) + def invert(x): + with np.errstate(divide='ignore'): + return 1 / x + + axsec = ax.secondary_xaxis('top', functions=(invert, invert)) + fig.canvas.draw() + fig.set_size_inches((7, 4)) + assert_allclose(ax.get_position().extents, [0.125, 0.1, 0.9, 0.9])