Skip to content

ENH: make ax.get_position apply aspect #9855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/api/api_changes/2017-11-25-JMK.rst
Original file line number Diff line number Diff line change
@@ -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)``.
1 change: 1 addition & 0 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down