diff --git a/doc/api/api_changes/2017-11-25-JMK.rst b/doc/api/api_changes/2017-11-25-JMK.rst new file mode 100644 index 000000000000..333e7c29b8d6 --- /dev/null +++ b/doc/api/api_changes/2017-11-25-JMK.rst @@ -0,0 +1,8 @@ +`.Axes.get_position` now returns actual position if aspect changed +------------------------------------------------------------------ + +`.Axes.get_position` used to return the original position unless a +draw had been triggered or `.Axes.apply_aspect` had been called, even +if the kwarg *original* was set to *False*. Now `.Axes.apply_aspect` +is called so ``ax.get_position()`` will return the new modified position. +To get the old behaviour, ``ax.get_position(original=True)``. diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index f7819aaf9fd8..b7eff33f1985 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -859,6 +859,7 @@ def get_position(self, original=False): if original: return self._originalPosition.frozen() else: + self.apply_aspect() return self._position.frozen() def set_position(self, pos, which='both'): diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index c0a56570a542..f1a8e2c0c610 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1164,7 +1164,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15, # transform each of the axes in parents using the new transform for ax in parents: - new_posn = shrinking_trans.transform(ax.get_position()) + new_posn = shrinking_trans.transform(ax.get_position(original=True)) new_posn = mtransforms.Bbox(new_posn) ax.set_position(new_posn) if parent_anchor is not False: diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 6f3d513e4fbf..3c21dc1d850b 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -24,7 +24,8 @@ import matplotlib.markers as mmarkers import matplotlib.patches as mpatches import matplotlib.colors as mcolors -from numpy.testing import assert_allclose, assert_array_equal +from numpy.testing import ( + assert_allclose, assert_array_equal, assert_array_almost_equal) from matplotlib.cbook import ( IgnoredKeywordWarning, MatplotlibDeprecationWarning) from matplotlib.cbook._backports import broadcast_to @@ -4828,6 +4829,12 @@ def test_square_plot(): xlim, ylim = ax.get_xlim(), ax.get_ylim() assert np.diff(xlim) == np.diff(ylim) assert ax.get_aspect() == 'equal' + assert_array_almost_equal( + ax.get_position(original=True).extents, + np.array((0.125, 0.1, 0.9, 0.9))) + assert_array_almost_equal( + ax.get_position(original=False).extents, + np.array((0.2125, 0.1, 0.8125, 0.9))) def test_no_None():