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

Skip to content

Commit 2cc8e14

Browse files
committed
Only allow set_adjustable("datalim") for axes with standard data ratios.
If an Axes subclass overrides get_data_ratio to anything else than "ratio of data limits after application of scales" (e.g. Polar always returns 1), then the data limits manipulation in apply_aspect won't achieve the proper data ratio anyways, so disable setting adjustable to "datalim" in that case. For example, calling `axis("equal")` on polar axes (which also sets adjustable to "datalim") will now error out instead of resulting in a nonsensical plot.
1 parent 2d56d42 commit 2cc8e14

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,11 +1317,19 @@ def set_adjustable(self, adjustable, share=False):
13171317
"""
13181318
cbook._check_in_list(["box", "datalim"], adjustable=adjustable)
13191319
if share:
1320-
axes = {*self._shared_x_axes.get_siblings(self),
1321-
*self._shared_y_axes.get_siblings(self)}
1320+
axs = {*self._shared_x_axes.get_siblings(self),
1321+
*self._shared_y_axes.get_siblings(self)}
13221322
else:
1323-
axes = [self]
1324-
for ax in axes:
1323+
axs = [self]
1324+
if (adjustable == "datalim"
1325+
and any(getattr(ax.get_data_ratio, "__func__")
1326+
!= _AxesBase.get_data_ratio
1327+
for ax in axs)):
1328+
# Limits adjustment by apply_aspect assumes that the axes' aspect
1329+
# ratio can be computed from the data limits and scales.
1330+
raise ValueError("Cannot set axes adjustable to 'datalim' for "
1331+
"Axes which override 'get_data_ratio'")
1332+
for ax in axs:
13251333
ax._adjustable = adjustable
13261334
self.stale = True
13271335

lib/matplotlib/tests/test_axes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4724,6 +4724,12 @@ def test_shared_with_aspect_3():
47244724
assert round(expected, 4) == round(ax.get_aspect(), 4)
47254725

47264726

4727+
def test_polar_not_datalim_adjustable():
4728+
ax = plt.figure().add_subplot(projection="polar")
4729+
with pytest.raises(ValueError):
4730+
ax.set_adjustable("datalim")
4731+
4732+
47274733
@pytest.mark.parametrize('twin', ('x', 'y'))
47284734
def test_twin_with_aspect(twin):
47294735
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)