Skip to content

Commit 0e024ff

Browse files
committed
fix #7908
1 parent 357dad0 commit 0e024ff

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff 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

lib/matplotlib/figure.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from matplotlib import rcParams
2525
from matplotlib import docstring
2626
from matplotlib import __version__ as _mpl_version
27+
from matplotlib import get_backend
2728

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

lib/matplotlib/tests/test_backend_bases.py

Lines changed: 19 additions & 0 deletions
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():
@@ -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)

0 commit comments

Comments
 (0)