diff --git a/matplotlib_inline/backend_inline.py b/matplotlib_inline/backend_inline.py index 0e148ac..cc1bfd5 100644 --- a/matplotlib_inline/backend_inline.py +++ b/matplotlib_inline/backend_inline.py @@ -96,7 +96,9 @@ def show(close=None, block=None): # only call close('all') if any to close # close triggers gc.collect, which can be slow if close and Gcf.get_all_fig_managers(): - matplotlib.pyplot.close("all") + from matplotlib import pyplot + + pyplot.close("all") # This flag will be reset by draw_if_interactive when called diff --git a/tests/test_import.py b/tests/test_import.py index c816b29..56e3285 100644 --- a/tests/test_import.py +++ b/tests/test_import.py @@ -2,3 +2,17 @@ def test_import(): from matplotlib_inline.backend_inline import show show() + + +def test_show_closes_figures_without_pyplot_attribute(monkeypatch): + # gh-24: lazy loaders (e.g. librosa < 0.10.1) can leave matplotlib.pyplot + # in sys.modules without setting it as an attribute of matplotlib. + import matplotlib + import matplotlib.pyplot as plt + + from matplotlib_inline.backend_inline import show + + plt.figure() + monkeypatch.delattr(matplotlib, "pyplot") + show(close=True) + assert plt.get_fignums() == []