File tree 3 files changed +34
-7
lines changed
3 files changed +34
-7
lines changed Original file line number Diff line number Diff line change @@ -179,13 +179,20 @@ def show(cls, block=None):
179
179
is ``None`` and we are neither in IPython's ``%pylab`` mode, nor in
180
180
`interactive` mode.
181
181
"""
182
- if cls .mainloop is None :
183
- return
184
182
managers = Gcf .get_all_fig_managers ()
185
183
if not managers :
186
184
return
187
185
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
189
196
if block is None :
190
197
# Hack: Are we in IPython's pylab mode?
191
198
from matplotlib import pyplot
Original file line number Diff line number Diff line change 19
19
from matplotlib import rcParams
20
20
from matplotlib import docstring
21
21
from matplotlib import __version__ as _mpl_version
22
+ from matplotlib import get_backend
22
23
23
24
import matplotlib .artist as martist
24
25
from matplotlib .artist import Artist , allow_rasterization
@@ -414,7 +415,7 @@ def show(self, warn=True):
414
415
415
416
Parameters
416
417
----------
417
- warm : bool
418
+ warn : bool
418
419
If ``True``, issue warning when called on a non-GUI backend
419
420
420
421
Notes
@@ -437,10 +438,10 @@ def show(self, warn=True):
437
438
except NonGuiException :
438
439
pass
439
440
if warn :
440
- import warnings
441
441
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 ())
444
445
445
446
def _get_axes (self ):
446
447
return self ._axstack .as_list ()
Original file line number Diff line number Diff line change 9
9
import os
10
10
import shutil
11
11
import tempfile
12
+ import pytest
12
13
13
14
14
15
def test_uses_per_path ():
@@ -60,3 +61,21 @@ def test_get_default_filename():
60
61
assert filename == 'image.png'
61
62
finally :
62
63
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