Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b8995e0

Browse files
authored
Merge pull request #12512 from jklymak/auto-backport-of-pr-12363-on-v3.0.x
Backport PR #12363 on branch v3.0.x
2 parents 9c9a660 + 65bedc5 commit b8995e0

File tree

7 files changed

+53
-9
lines changed

7 files changed

+53
-9
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,9 @@ def get_position(self, original=False):
839839
if original:
840840
return self._originalPosition.frozen()
841841
else:
842-
self.apply_aspect()
842+
locator = self.get_axes_locator()
843+
if not locator:
844+
self.apply_aspect()
843845
return self._position.frozen()
844846

845847
def set_position(self, pos, which='both'):
@@ -861,7 +863,7 @@ def set_position(self, pos, which='both'):
861863
Determines which position variables to change.
862864
863865
"""
864-
self._set_position(pos, which='both')
866+
self._set_position(pos, which=which)
865867
# because this is being called externally to the library we
866868
# zero the constrained layout parts.
867869
self._layoutbox = None

lib/matplotlib/figure.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,9 +2271,9 @@ def get_tightbbox(self, renderer, bbox_extra_artists=None):
22712271
if bbox is not None and (bbox.width != 0 or bbox.height != 0):
22722272
bb.append(bbox)
22732273

2274-
for ax in self.axes:
2275-
if ax.get_visible():
2276-
bb.append(ax.get_tightbbox(renderer, bbox_extra_artists))
2274+
bb.extend(
2275+
ax.get_tightbbox(renderer, bbox_extra_artists=bbox_extra_artists)
2276+
for ax in self.axes if ax.get_visible())
22772277

22782278
if len(bb) == 0:
22792279
return self.bbox_inches

lib/matplotlib/tests/test_axes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5781,6 +5781,19 @@ def test_zoom_inset():
57815781
xx, rtol=1e-4)
57825782

57835783

5784+
def test_set_position():
5785+
fig, ax = plt.subplots()
5786+
ax.set_aspect(3.)
5787+
ax.set_position([0.1, 0.1, 0.4, 0.4], which='both')
5788+
assert np.allclose(ax.get_position().width, 0.1)
5789+
ax.set_aspect(2.)
5790+
ax.set_position([0.1, 0.1, 0.4, 0.4], which='original')
5791+
assert np.allclose(ax.get_position().width, 0.15)
5792+
ax.set_aspect(3.)
5793+
ax.set_position([0.1, 0.1, 0.4, 0.4], which='active')
5794+
assert np.allclose(ax.get_position().width, 0.1)
5795+
5796+
57845797
def test_spines_properbbox_after_zoom():
57855798
fig, ax = plt.subplots()
57865799
bb = ax.spines['bottom'].get_window_extent(fig.canvas.get_renderer())

lib/mpl_toolkits/axes_grid1/axes_size.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ def __init__(self, ax, direction):
321321
raise KeyError("direction must be one of left, right, bottom, top")
322322

323323
def __call__(self, renderer):
324-
vl = [self._get_func(ax.get_tightbbox(renderer, False),
325-
ax.bbox) for ax in self._ax_list]
324+
vl = [self._get_func(ax.get_tightbbox(renderer,
325+
call_axes_locator=False),
326+
ax.bbox)
327+
for ax in self._ax_list]
326328
return max(vl)

lib/mpl_toolkits/axes_grid1/parasite_axes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,10 @@ def _remove_method(h):
329329
return ax2
330330

331331
def get_tightbbox(self, renderer, call_axes_locator=True):
332-
bbs = [ax.get_tightbbox(renderer, call_axes_locator)
332+
bbs = [ax.get_tightbbox(renderer, call_axes_locator=call_axes_locator)
333333
for ax in self.parasites]
334-
bbs.append(super().get_tightbbox(renderer, call_axes_locator))
334+
bbs.append(super().get_tightbbox(renderer,
335+
call_axes_locator=call_axes_locator))
335336
return Bbox.union([b for b in bbs if b.width != 0 or b.height != 0])
336337

337338

lib/mpl_toolkits/tests/test_axes_grid1.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from mpl_toolkits.axes_grid1 import host_subplot
66
from mpl_toolkits.axes_grid1 import make_axes_locatable
77
from mpl_toolkits.axes_grid1 import AxesGrid
8+
from mpl_toolkits.axes_grid1 import ImageGrid
89
from mpl_toolkits.axes_grid1.inset_locator import (
910
zoomed_inset_axes,
1011
mark_inset,
@@ -381,3 +382,28 @@ def test_anchored_direction_arrows_many_args():
381382
sep_x=-0.06, sep_y=-0.08, back_length=0.1, head_width=9,
382383
head_length=10, tail_width=5)
383384
ax.add_artist(direction_arrows)
385+
386+
387+
def test_axes_locatable_position():
388+
fig, ax = plt.subplots()
389+
divider = make_axes_locatable(ax)
390+
cax = divider.append_axes('right', size='5%', pad='2%')
391+
fig.canvas.draw()
392+
assert np.isclose(cax.get_position(original=False).width,
393+
0.03621495327102808)
394+
395+
396+
@image_comparison(baseline_images=['image_grid'], extensions=['png'],
397+
remove_text=True, style='mpl20',
398+
savefig_kwarg={'bbox_inches': 'tight'})
399+
def test_image_grid():
400+
# test that image grid works with bbox_inches=tight.
401+
im = np.arange(100)
402+
im.shape = 10, 10
403+
404+
fig = plt.figure(1, (4., 4.))
405+
grid = ImageGrid(fig, 111, nrows_ncols=(2, 2), axes_pad=0.1)
406+
407+
for i in range(4):
408+
grid[i].imshow(im)
409+
grid[i].set_title('test {0}{0}'.format(i))

0 commit comments

Comments
 (0)