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 @@ -182,13 +182,20 @@ def show(cls, block=None):
182
182
is ``None`` and we are neither in IPython's ``%pylab`` mode, nor in
183
183
`interactive` mode.
184
184
"""
185
- if cls .mainloop is None :
186
- return
187
185
managers = Gcf .get_all_fig_managers ()
188
186
if not managers :
189
187
return
190
188
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
192
199
if block is None :
193
200
# Hack: Are we in IPython's pylab mode?
194
201
from matplotlib import pyplot
Original file line number Diff line number Diff line change 24
24
from matplotlib import rcParams
25
25
from matplotlib import docstring
26
26
from matplotlib import __version__ as _mpl_version
27
+ from matplotlib import get_backend
27
28
28
29
import matplotlib .artist as martist
29
30
from matplotlib .artist import Artist , allow_rasterization
@@ -431,7 +432,7 @@ def show(self, warn=True):
431
432
432
433
Parameters
433
434
----------
434
- warm : bool
435
+ warn : bool
435
436
If ``True``, issue warning when called on a non-GUI backend
436
437
437
438
Notes
@@ -454,10 +455,10 @@ def show(self, warn=True):
454
455
except NonGuiException :
455
456
pass
456
457
if warn :
457
- import warnings
458
458
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 ())
461
462
462
463
def _get_axes (self ):
463
464
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 ():
@@ -77,3 +78,21 @@ def test_get_default_filename_already_exists():
77
78
assert filename == 'image-1.png'
78
79
finally :
79
80
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