Skip to content

Commit 02646cb

Browse files
committed
Merge pull request #10685 from CinnyCao/becky-fix-#7908
fix plt.show doesn't warn if a non-GUI backend
2 parents b5b0003 + a2a145f commit 02646cb

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

lib/matplotlib/backend_bases.py

+10-3
Original file line numberDiff line numberDiff 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

lib/matplotlib/figure.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from matplotlib import rcParams
2020
from matplotlib import docstring
2121
from matplotlib import __version__ as _mpl_version
22+
from matplotlib import get_backend
2223

2324
import matplotlib.artist as martist
2425
from 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()

lib/matplotlib/tests/test_backend_bases.py

+19
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import shutil
1111
import tempfile
12+
import pytest
1213

1314

1415
def 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)

0 commit comments

Comments
 (0)