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

Skip to content

Commit 0903dcb

Browse files
tacaswellefiring
authored andcommitted
API: re-allow 'datalim' as a valid adjustable with shared axes
This is to allow twinx and twiny to work as expected.
1 parent bc20617 commit 0903dcb

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ def __init__(self, fig, rect,
440440
================ =========================================
441441
Keyword Description
442442
================ =========================================
443-
*adjustable* [ 'box' | 'datalim' | 'box-forced']
443+
*adjustable* [ 'box' | 'datalim' ]
444444
*alpha* float: the alpha transparency (can be None)
445445
*anchor* [ 'C', 'SW', 'S', 'SE', 'E', 'NE', 'N',
446446
'NW', 'W' ]
@@ -1348,10 +1348,6 @@ def set_adjustable(self, adjustable, share=False):
13481348
"""
13491349
# FIXME: add box-forced deprecation
13501350
if adjustable in ('box', 'datalim', 'box-forced'):
1351-
if self in self._shared_x_axes and self in self._shared_y_axes:
1352-
if adjustable == 'datalim':
1353-
raise ValueError(
1354-
'adjustable must be "box" when both axes are shared')
13551351
if share and self in self._shared_x_axes:
13561352
for ax in self._shared_x_axes.get_siblings(self):
13571353
ax._adjustable = adjustable
@@ -4121,6 +4117,9 @@ def _make_twin_axes(self, *kl, **kwargs):
41214117
make a twinx axes of self. This is used for twinx and twiny.
41224118
"""
41234119
ax2 = self.figure.add_axes(self.get_position(True), *kl, **kwargs)
4120+
# do not touch every thing shared, just this and it's twin.
4121+
self.set_adjustable('datalim')
4122+
ax2.set_adjustable('datalim')
41244123
return ax2
41254124

41264125
def twinx(self):

lib/matplotlib/tests/test_axes.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4449,8 +4449,6 @@ def test_shared_with_aspect():
44494449
plt.draw() # Trigger apply_aspect().
44504450
assert axes[0].get_xlim() == axes[1].get_xlim()
44514451
assert axes[0].get_ylim() == axes[1].get_ylim()
4452-
with pytest.raises(ValueError):
4453-
axes[0].set_aspect(1, adjustable='datalim', share=True)
44544452

44554453
# Different aspect ratios:
44564454
for adjustable in ['box', 'datalim']:
@@ -4471,6 +4469,17 @@ def test_shared_with_aspect():
44714469
assert round(expected, 4) == round(ax.get_aspect(), 4)
44724470

44734471

4472+
@pytest.mark.parametrize('twin', ('x', 'y'))
4473+
def test_twin_with_aspect(twin):
4474+
fig, ax = plt.subplots()
4475+
# test twinx or twiny
4476+
ax_twin = getattr(ax, 'twin{}'.format(twin))()
4477+
ax.set_aspect(5)
4478+
ax_twin.set_aspect(2)
4479+
assert_array_equal(ax.bbox.extents,
4480+
ax_twin.bbox.extents)
4481+
4482+
44744483
def test_relim_visible_only():
44754484
x1 = (0., 10.)
44764485
y1 = (0., 10.)

0 commit comments

Comments
 (0)