diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index a2a40567628b..be4adfb521ef 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -1282,20 +1282,25 @@ def tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None): def box(on=None): """ - Turn the axes box on or off. + Turn the axes box on or off on the current axes. Parameters ---------- on : bool or None - The new axes box state. If ``None``, toggle the state. + The new `~matplotlib.axes.Axes` box state. If ``None``, toggle + the state. + + See Also + -------- + :meth:`matplotlib.axes.Axes.set_frame_on` + :meth:`matplotlib.axes.Axes.get_frame_on` """ ax = gca() - on = _string_to_bool(on) if on is None: on = not ax.get_frame_on() + on = _string_to_bool(on) ax.set_frame_on(on) - ## Axis ## diff --git a/lib/matplotlib/tests/test_pyplot.py b/lib/matplotlib/tests/test_pyplot.py index e939d440060b..b3bb30474f04 100644 --- a/lib/matplotlib/tests/test_pyplot.py +++ b/lib/matplotlib/tests/test_pyplot.py @@ -19,3 +19,15 @@ def test_pyplot_up_to_date(): assert orig_contents == new_contents finally: Path(plt.__file__).write_text(orig_contents) + + +def test_pyplot_box(): + fig, ax = plt.subplots() + plt.box(False) + assert not ax.get_frame_on() + plt.box(True) + assert ax.get_frame_on() + plt.box() + assert not ax.get_frame_on() + plt.box() + assert ax.get_frame_on()