8000 Merge pull request #23535 from oscargus/marginserror · matplotlib/matplotlib@60b4104 · GitHub
[go: up one dir, main page]

Skip to content

Commit 60b4104

Browse files
authored
Merge pull request #23535 from oscargus/marginserror
Make margins error as claimed in doc-string
2 parents ddc260c + f07b234 commit 60b4104

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2705,7 +2705,7 @@ def margins(self, *margins, x=None, y=None, tight=True):
27052705
before calling :meth:`margins`.
27062706
"""
27072707

2708-
if margins and x is not None and y is not None:
2708+
if margins and (x is not None or y is not None):
27092709
raise TypeError('Cannot pass both positional and keyword '
27102710
'arguments for x and/or y.')
27112711
elif len(margins) == 1:

lib/matplotlib/tests/test_axes.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5732,21 +5732,17 @@ def test_set_margin_updates_limits():
57325732
assert ax.get_xlim() == (1, 2)
57335733

57345734

5735-
@pytest.mark.parametrize('err, args, kwargs, match',
5736-
((ValueError, (-1,), {},
5737-
'margin must be greater than -0.5'),
5738-
(ValueError, (1, -1), {},
5739-
'margin must be greater than -0.5'),
5740-
(ValueError, tuple(), {'x': -1},
5741-
'margin must be greater than -0.5'),
5742-
(ValueError, tuple(), {'y': -1},
5743-
'margin must be greater than -0.5'),
5744-
(TypeError, (1, ), {'x': 1, 'y': 1},
5745-
'Cannot pass both positional and keyword '
5746-
'arguments for x and/or y.'),
5747-
(TypeError, (1, 1, 1), {},
5748-
'Must pass a single positional argument for all*'),
5749-
))
5735+
@pytest.mark.parametrize('err, args, kwargs, match', (
5736+
(ValueError, (-1,), {}, r'margin must be greater than -0\.5'),
5737+
(ValueError, (1, -1), {}, r'margin must be greater than -0\.5'),
5738+
(ValueError, tuple(), {'x': -1}, r'margin must be greater than -0\.5'),
5739+
(ValueError, tuple(), {'y': -1}, r'margin must be greater than -0\.5'),
5740+
(TypeError, (1, ), {'x': 1, 'y': 1},
5741+
'Cannot pass both positional and keyword arguments for x and/or y'),
5742+
(TypeError, (1, ), {'x': 1},
5743+
'Cannot pass both positional and keyword arguments for x and/or y'),
5744+
(TypeError, (1, 1, 1), {}, 'Must pass a single positional argument'),
5745+
))
57505746
def test_margins_errors(err, args, kwargs, match):
57515747
with pytest.raises(err, match=match):
57525748
fig = plt.figure()

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ def margins(self, *margins, x=None, y=None, z=None, tight=True):
488488
applies to 3D Axes, it also takes a *z* argument, and returns
489489
``(xmargin, ymargin, zmargin)``.
490490
"""
491-
if margins and x is not None and y is not None and z is not None:
491+
if margins and (x is not None or y is not None or z is not None):
492492
raise TypeError('Cannot pass both positional and keyword '
493493
'arguments for x, y, and/or z.')
494494
elif len(margins) == 1:

lib/mpl_toolkits/tests/test_mplot3d.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,13 +1715,26 @@ def test_margins():
17151715
assert ax.margins() == (0, 0.1, 0)
17161716

17171717

1718-
def test_margins_errors():
1719-
fig = plt.figure()
1720-
ax = fig.add_subplot(projection='3d')
1721-
with pytest.raises(TypeError, match="Cannot pass"):
1722-
ax.margins(0.2, x=0.4, y=0.4, z=0.4)
1723-
with pytest.raises(TypeError, match="Must pass"):
1724-
ax.margins(0.2, 0.4)
1718+
@pytest.mark.parametrize('err, args, kwargs, match', (
1719+
(ValueError, (-1,), {}, r'margin must be greater than -0\.5'),
1720+
(ValueError, (1, -1, 1), {}, r'margin must be greater than -0\.5'),
1721+
(ValueError, (1, 1, -1), {}, r'margin must be greater than -0\.5'),
1722+
(ValueError, tuple(), {'x': -1}, r'margin must be greater than -0\.5'),
1723+
(ValueError, tuple(), {'y': -1}, r'margin must be greater than -0\.5'),
1724+
(ValueError, tuple(), {'z': -1}, r'margin must be greater than -0\.5'),
1725+
(TypeError, (1, ), {'x': 1},
1726+
'Cannot pass both positional and keyword'),
1727+
(TypeError, (1, ), {'x': 1, 'y': 1, 'z': 1},
1728+
'Cannot pass both positional and keyword'),
1729+
(TypeError, (1, ), {'x': 1, 'y': 1},
1730+
'Cannot pass both positional and keyword'),
1731+
(TypeError, (1, 1), {}, 'Must pass a single positional argument for'),
1732+
))
1733+
def test_margins_errors(err, args, kwargs, match):
1734+
with pytest.raises(err, match=match):
1735+
fig = plt.figure()
1736+
ax = fig.add_subplot(projection='3d')
1737+
ax.margins(*args, **kwargs)
17251738

17261739

17271740
@check_figures_equal(extensions=["png"])

0 commit comments

Comments
 (0)
0