File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -182,13 +182,20 @@ def show(cls, block=None):
182182 is ``None`` and we are neither in IPython's ``%pylab`` mode, nor in
183183 `interactive` mode.
184184 """
185- if cls .mainloop is None :
186- return
187185 managers = Gcf .get_all_fig_managers ()
188186 if not managers :
189187 return
190188 for manager in managers :
191- manager .show ()
189+ try :
190+ manager .show ()
191+ except NonGuiException :
192+ warnings .warn (
193+ ('matplotlib is currently using %s, which is a ' +
194+ 'non-GUI backend, so cannot show the figure.' )
195+ % get_backend ())
196+ return
197+ if cls .mainloop is None :
198+ return
192199 if block is None :
193200 # Hack: Are we in IPython's pylab mode?
194201 from matplotlib import pyplot
Original file line number Diff line number Diff line change 2424from matplotlib import rcParams
2525from matplotlib import docstring
2626from matplotlib import __version__ as _mpl_version
27+ from matplotlib import get_backend
2728
2829import matplotlib .artist as martist
2930from matplotlib .artist import Artist , allow_rasterization
@@ -431,7 +432,7 @@ def show(self, warn=True):
431432
432433 Parameters
433434 ----------
434- warm : bool
435+ warn : bool
435436 If ``True``, issue warning when called on a non-GUI backend
436437
437438 Notes
@@ -454,10 +455,10 @@ def show(self, warn=True):
454455 except NonGuiException :
455456 pass
456457 if warn :
457- import warnings
458458 warnings .warn (
459- "matplotlib is currently using a non-GUI backend, "
460- "so cannot show the figure" )
459+ ('matplotlib is currently using %s, which is a ' +
460+ 'non-GUI backend, so cannot show the figure.' )
461+ % get_backend ())
461462
462463 def _get_axes (self ):
463464 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 ():
@@ -77,3 +78,21 @@ def test_get_default_filename_already_exists():
7778 assert filename == 'image-1.png'
7879 finally :
7980 shutil .rmtree (test_dir )
81+
82+
83+ @pytest .mark .backend ('pdf' )
84+ def test_non_gui_warning ():
85+ plt .subplots ()
86+ with pytest .warns (UserWarning ) as rec :
87+ plt .show ()
88+ assert len (rec ) == 1
89+ assert 'matplotlib is currently using pdf, ' \
90+ 'which is a non-GUI backend' \
91+ in str (rec [0 ].message )
92+
93+ with pytest .warns (UserWarning ) as rec :
94+ plt .gcf ().show ()
95+ assert len (rec ) == 1
96+ assert 'matplotlib is currently using pdf, ' \
97+ 'which is a non-GUI backend' \
98+ in str (rec [0 ].message )
You can’t perform that action at this time.
0 commit comments