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

Skip to content

Commit ff53308

Browse files
authored
Merge pull request #12363 from jklymak/fix-correct-get-position-errors
FIX: errors in get_position changes
2 parents aa1e5cf + 78db75f commit ff53308

File tree

7 files changed

+51
-8
lines changed

7 files changed

+51
-8
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,9 @@ def get_position(self, original=False):
835835
if original:
836836
return self._originalPosition.frozen()
837837
else:
838-
self.apply_aspect()
838+
locator = self.get_axes_locator()
839+
if not locator:
840+
self.apply_aspect()
839841
return self._position.frozen()
840842

841843
def set_position(self, pos, which='both'):
@@ -857,7 +859,7 @@ def set_position(self, pos, which='both'):
857859
Determines which position variables to change.
858860
859861
"""
860-
self._set_position(pos, which='both')
862+
self._set_position(pos, which=which)
861863
# because this is being called externally to the library we
862864
# zero the constrained layout parts.
863865
self._layoutbox = None

lib/matplotlib/figure.py

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

2299-
for ax in self.axes:
2300-
if ax.get_visible():
2301-
bb.append(ax.get_tightbbox(renderer, bbox_extra_artists))
2299+
bb.extend(
2300+
ax.get_tightbbox(renderer, bbox_extra_artists=bbox_extra_artists)
2301+
for ax in self.axes if ax.get_visible())
23022302

23032303
if len(bb) == 0:
23042304
return self.bbox_inches

lib/matplotlib/tests/test_axes.py

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

57915791

5792+
def test_set_position():
5793+
fig, ax = plt.subplots()
5794+
ax.set_aspect(3.)
5795+
ax.set_position([0.1, 0.1, 0.4, 0.4], which='both')
5796+
assert np.allclose(ax.get_position().width, 0.1)
5797+
ax.set_aspect(2.)
5798+
ax.set_position([0.1, 0.1, 0.4, 0.4], which='original')
5799+
assert np.allclose(ax.get_position().width, 0.15)
5800+
ax.set_aspect(3.)
5801+
ax.set_position([0.1, 0.1, 0.4, 0.4], which='active')
5802+
assert np.allclose(ax.get_position().width, 0.1)
5803+
5804+
57925805
def test_spines_properbbox_after_zoom():
57935806
fig, ax = plt.subplots()
57945807
bb = ax.spines['bottom'].get_window_extent(fig.canvas.get_renderer())

lib/mpl_toolkits/axes_grid1/axes_size.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ def __init__(self, ax, direction):
305305

306306
def __call__(self, renderer):
307307
get_func = self._get_func_map[self._direction]
308-
vl = [get_func(ax.get_tightbbox(renderer, False), ax.bbox)
308+
vl = [get_func(ax.get_tightbbox(renderer, call_axes_locator=False),
309+
ax.bbox)
309310
for ax in self._ax_list]
310311
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
@@ -327,9 +327,10 @@ def _remove_method(h):
327327
return ax2
328328

329329
def get_tightbbox(self, renderer, call_axes_locator=True):
330-
bbs = [ax.get_tightbbox(renderer, call_axes_locator)
330+
bbs = [ax.get_tightbbox(renderer, call_axes_locator=call_axes_locator)
331331
for ax in self.parasites]
332-
bbs.append(super().get_tightbbox(renderer, call_axes_locator))
332+
bbs.append(super().get_tightbbox(renderer,
333+
call_axes_locator=call_axes_locator))
333334
return Bbox.union([b for b in bbs if b.width != 0 or b.height != 0])
334335

335336

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,
@@ -380,3 +381,28 @@ def test_anchored_direction_arrows_many_args():
380381
sep_x=-0.06, sep_y=-0.08, back_length=0.1, head_width=9,
381382
head_length=10, tail_width=5)
382383
ax.add_artist(direction_arrows)
384+
385+
386+
def test_axes_locatable_position():
387+
fig, ax = plt.subplots()
388+
divider = make_axes_locatable(ax)
389+
cax = divider.append_axes('right', size='5%', pad='2%')
390+
fig.canvas.draw()
391+
assert np.isclose(cax.get_position(original=False).width,
392+
0.03621495327102808)
393+
394+
395+
@image_comparison(baseline_images=['image_grid'], extensions=['png'],
396+
remove_text=True, style='mpl20',
397+
savefig_kwarg={'bbox_inches': 'tight'})
398+
def test_image_grid():
399+
# test that image grid works with bbox_inches=tight.
400+
im = np.arange(100)
401+
im.shape = 10, 10
402+
403+
fig = plt.figure(1, (4., 4.))
404+
grid = ImageGrid(fig, 111, nrows_ncols=(2, 2), axes_pad=0.1)
405+
406+
for i in range(4):
407+
grid[i].imshow(im)
408+
grid[i].set_title('test {0}{0}'.format(i))

0 commit comments

Comments
 (0)