File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -179,13 +179,20 @@ def show(cls, block=None):
179179 is ``None`` and we are neither in IPython's ``%pylab`` mode, nor in
180180 `interactive` mode.
181181 """
182- if cls .mainloop is None :
183- return
184182 managers = Gcf .get_all_fig_managers ()
185183 if not managers :
186184 return
187185 for manager in managers :
188- manager .show ()
186+ try :
187+ manager .show ()
188+ except NonGuiException :
189+ warnings .warn (
190+ ('matplotlib is currently using %s, which is a ' +
191+ 'non-GUI backend, so cannot show the figure.' )
192+ % get_backend ())
193+ return
194+ if cls .mainloop is None :
195+ return
189196 if block is None :
190197 # Hack: Are we in IPython's pylab mode?
191198 from matplotlib import pyplot
Original file line number Diff line number Diff line change 1919from matplotlib import rcParams
2020from matplotlib import docstring
2121from matplotlib import __version__ as _mpl_version
22+ from matplotlib import get_backend
2223
2324import matplotlib .artist as martist
2425from matplotlib .artist import Artist , allow_rasterization
@@ -414,7 +415,7 @@ def show(self, warn=True):
414415
415416 Parameters
416417 ----------
417- warm : bool
418+ warn : bool
418419 If ``True``, issue warning when called on a non-GUI backend
419420
420421 Notes
@@ -437,10 +438,10 @@ def show(self, warn=True):
437438 except NonGuiException :
438439 pass
439440 if warn :
440- import warnings
441441 warnings .warn (
442- "matplotlib is currently using a non-GUI backend, "
443- "so cannot show the figure" )
442+ ('matplotlib is currently using %s, which is a ' +
443+ 'non-GUI backend, so cannot show the figure.' )
444+ % get_backend ())
444445
445446 def _get_axes (self ):
446447 return self ._axstack .as_list ()
Original file line number Diff line number Diff line change 99import os
1010import shutil
1111import tempfile
12+ import pytest
1213
1314
1415def test_uses_per_path ():
@@ -60,3 +61,21 @@ def test_get_default_filename():
6061 assert filename == 'image.png'
6162 finally :
6263 shutil .rmtree (test_dir )
64+
65+
66+ @pytest .mark .backend ('pdf' )
67+ def test_non_gui_warning ():
68+ plt .subplots ()
69+ with pytest .warns (UserWarning ) as rec :
70+ plt .show ()
71+ assert len (rec ) == 1
72+ assert 'matplotlib is currently using pdf, ' \
73+ 'which is a non-GUI backend' \
74+ in str (rec [0 ].message )
75+
76+ with pytest .warns (UserWarning ) as rec :
77+ plt .gcf ().show ()
78+ assert len (rec ) == 1
79+ assert 'matplotlib is currently using pdf, ' \
80+ 'which is a non-GUI backend' \
81+ in str (rec [0 ].message )
You can’t perform that action at this time.
0 commit comments