diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 05a3c824bcbf..3c9357106950 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -839,7 +839,9 @@ def get_position(self, original=False): if original: return self._originalPosition.frozen() else: - self.apply_aspect() + locator = self.get_axes_locator() + if not locator: + self.apply_aspect() return self._position.frozen() def set_position(self, pos, which='both'): @@ -861,7 +863,7 @@ def set_position(self, pos, which='both'): Determines which position variables to change. """ - self._set_position(pos, which='both') + self._set_position(pos, which=which) # because this is being called externally to the library we # zero the constrained layout parts. self._layoutbox = None diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index baed622914bc..fa1a1849142e 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -2271,9 +2271,9 @@ def get_tightbbox(self, renderer, bbox_extra_artists=None): if bbox is not None and (bbox.width != 0 or bbox.height != 0): bb.append(bbox) - for ax in self.axes: - if ax.get_visible(): - bb.append(ax.get_tightbbox(renderer, bbox_extra_artists)) + bb.extend( + ax.get_tightbbox(renderer, bbox_extra_artists=bbox_extra_artists) + for ax in self.axes if ax.get_visible()) if len(bb) == 0: return self.bbox_inches diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index b7b478fa75d9..959121c30a4b 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -5781,6 +5781,19 @@ def test_zoom_inset(): xx, rtol=1e-4) +def test_set_position(): + fig, ax = plt.subplots() + ax.set_aspect(3.) + ax.set_position([0.1, 0.1, 0.4, 0.4], which='both') + assert np.allclose(ax.get_position().width, 0.1) + ax.set_aspect(2.) + ax.set_position([0.1, 0.1, 0.4, 0.4], which='original') + assert np.allclose(ax.get_position().width, 0.15) + ax.set_aspect(3.) + ax.set_position([0.1, 0.1, 0.4, 0.4], which='active') + assert np.allclose(ax.get_position().width, 0.1) + + def test_spines_properbbox_after_zoom(): fig, ax = plt.subplots() bb = ax.spines['bottom'].get_window_extent(fig.canvas.get_renderer()) diff --git a/lib/mpl_toolkits/axes_grid1/axes_size.py b/lib/mpl_toolkits/axes_grid1/axes_size.py index 64558e3393fa..7ca6784ce467 100644 --- a/lib/mpl_toolkits/axes_grid1/axes_size.py +++ b/lib/mpl_toolkits/axes_grid1/axes_size.py @@ -321,6 +321,8 @@ def __init__(self, ax, direction): raise KeyError("direction must be one of left, right, bottom, top") def __call__(self, renderer): - vl = [self._get_func(ax.get_tightbbox(renderer, False), - ax.bbox) for ax in self._ax_list] + vl = [self._get_func(ax.get_tightbbox(renderer, + call_axes_locator=False), + ax.bbox) + for ax in self._ax_list] return max(vl) diff --git a/lib/mpl_toolkits/axes_grid1/parasite_axes.py b/lib/mpl_toolkits/axes_grid1/parasite_axes.py index d003e5c40253..be3570f9b49a 100644 --- a/lib/mpl_toolkits/axes_grid1/parasite_axes.py +++ b/lib/mpl_toolkits/axes_grid1/parasite_axes.py @@ -329,9 +329,10 @@ def _remove_method(h): return ax2 def get_tightbbox(self, renderer, call_axes_locator=True): - bbs = [ax.get_tightbbox(renderer, call_axes_locator) + bbs = [ax.get_tightbbox(renderer, call_axes_locator=call_axes_locator) for ax in self.parasites] - bbs.append(super().get_tightbbox(renderer, call_axes_locator)) + bbs.append(super().get_tightbbox(renderer, + call_axes_locator=call_axes_locator)) return Bbox.union([b for b in bbs if b.width != 0 or b.height != 0]) diff --git a/lib/mpl_toolkits/tests/baseline_images/test_axes_grid1/image_grid.png b/lib/mpl_toolkits/tests/baseline_images/test_axes_grid1/image_grid.png new file mode 100644 index 000000000000..c4a2aae5607e Binary files /dev/null and b/lib/mpl_toolkits/tests/baseline_images/test_axes_grid1/image_grid.png differ diff --git a/lib/mpl_toolkits/tests/test_axes_grid1.py b/lib/mpl_toolkits/tests/test_axes_grid1.py index 896ba503f91f..2ac9d7d089fb 100644 --- a/lib/mpl_toolkits/tests/test_axes_grid1.py +++ b/lib/mpl_toolkits/tests/test_axes_grid1.py @@ -5,6 +5,7 @@ from mpl_toolkits.axes_grid1 import host_subplot from mpl_toolkits.axes_grid1 import make_axes_locatable from mpl_toolkits.axes_grid1 import AxesGrid +from mpl_toolkits.axes_grid1 import ImageGrid from mpl_toolkits.axes_grid1.inset_locator import ( zoomed_inset_axes, mark_inset, @@ -381,3 +382,28 @@ def test_anchored_direction_arrows_many_args(): sep_x=-0.06, sep_y=-0.08, back_length=0.1, head_width=9, head_length=10, tail_width=5) ax.add_artist(direction_arrows) + + +def test_axes_locatable_position(): + fig, ax = plt.subplots() + divider = make_axes_locatable(ax) + cax = divider.append_axes('right', size='5%', pad='2%') + fig.canvas.draw() + assert np.isclose(cax.get_position(original=False).width, + 0.03621495327102808) + + +@image_comparison(baseline_images=['image_grid'], extensions=['png'], + remove_text=True, style='mpl20', + savefig_kwarg={'bbox_inches': 'tight'}) +def test_image_grid(): + # test that image grid works with bbox_inches=tight. + im = np.arange(100) + im.shape = 10, 10 + + fig = plt.figure(1, (4., 4.)) + grid = ImageGrid(fig, 111, nrows_ncols=(2, 2), axes_pad=0.1) + + for i in range(4): + grid[i].imshow(im) + grid[i].set_title('test {0}{0}'.format(i))