diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 62f64706b609..a1f25f9f9920 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1825,7 +1825,7 @@ def margins(self, *args, **kw): mx = my = args[0] elif len(args) == 2: mx, my = args - else: + elif len(args) > 2: raise ValueError("more than two arguments were supplied") if mx is not None: self.set_xmargin(mx) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 2cbb7054f832..ce6ef43b6051 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -3109,6 +3109,25 @@ def test_pie_ccw_true(): # Set aspect ratio to be equal so that pie is drawn as a circle. plt.axis('equal') +@cleanup +def test_margins(): + # test all ways margins can be called + data = [1, 10] + + fig1, ax1 = plt.subplots(1, 1) + ax1.plot(data) + ax1.margins(1) + assert_equal(ax1.margins(), (1, 1)) + + fig2, ax2 = plt.subplots(1, 1) + ax2.plot(data) + ax2.margins(1, 0.5) + assert_equal(ax2.margins(), (1, 0.5)) + + fig3, ax3 = plt.subplots(1, 1) + ax3.plot(data) + ax3.margins(x=1, y=0.5) + assert_equal(ax3.margins(), (1, 0.5)) @cleanup def test_pathological_hexbin():