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

Skip to content

Commit e761485

Browse files
authored
Merge pull request #23931 from ZzN1NJ4/main
Raise ValueError on negative number inputs for set_aspect
2 parents d099170 + 14662e0 commit e761485

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,8 @@ def set_aspect(self, aspect, adjustable=None, anchor=None, share=False):
16811681
aspect = 1
16821682
if not cbook._str_equal(aspect, 'auto'):
16831683
aspect = float(aspect) # raise ValueError if necessary
1684+
if aspect <= 0 or not np.isfinite(aspect):
1685+
raise ValueError("aspect must be finite and positive ")
16841686

16851687
if share:
16861688
axes = {sibling for name in self._axis_names

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7569,6 +7569,18 @@ def test_bbox_aspect_axes_init():
75697569
assert_allclose(sizes, sizes[0])
75707570

75717571

7572+
def test_set_aspect_negative():
7573+
fig, ax = plt.subplots()
7574+
with pytest.raises(ValueError, match="must be finite and positive"):
7575+
ax.set_aspect(-1)
7576+
with pytest.raises(ValueError, match="must be finite and positive"):
7577+
ax.set_aspect(0)
7578+
with pytest.raises(ValueError, match="must be finite and positive"):
7579+
ax.set_aspect(np.inf)
7580+
with pytest.raises(ValueError, match="must be finite and positive"):
7581+
ax.set_aspect(-np.inf)
7582+
7583+
75727584
def test_redraw_in_frame():
75737585
fig, ax = plt.subplots(1, 1)
75747586
ax.plot([1, 2, 3])

0 commit comments

Comments
 (0)