diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index d8e014c845e0..03c21da82de7 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -1034,6 +1034,24 @@ def get_backend(): "Returns the current backend." return rcParams['backend'] + +class NonGUIBackendWarning(Warning): + pass + + +def _warn_non_gui_show(): + warnings.warn( + "matplotlib is currently using a non-GUI backend, " + "so the figure can not be shown. Call " + "matplotlib.hide_show_warnings() to suppress " + "these warnings.", + NonGUIBackendWarning) + + +def hide_show_warnings(): + warnings.simplefilter('ignore', NonGUIBackendWarning) + + def interactive(b): """ Set interactive mode to boolean b. diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index c0eeb0543db6..ee3603a55aab 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -39,6 +39,7 @@ from matplotlib import rcParams from matplotlib import is_interactive from matplotlib._pylab_helpers import Gcf +from matplotlib import _warn_non_gui_show from matplotlib.transforms import Bbox, TransformedBbox, Affine2D import cStringIO @@ -2423,13 +2424,19 @@ def __init__(self, canvas, num): self.key_press) """ The returned id from connecting the default key handler via :meth:`FigureCanvasBase.mpl_connnect`. - + To disable default key press handling:: - + manager, canvas = figure.canvas.manager, figure.canvas canvas.mpl_disconnect(manager.key_press_handler_id) - + + """ + + def show(self): + """ + For GUI backends, show the figure window and redraw. """ + _warn_non_gui_show() def destroy(self): pass diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 2dafa2c5827b..78299c3c8a61 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -39,6 +39,7 @@ from matplotlib.text import Text, _process_text_args from matplotlib.transforms import (Affine2D, Bbox, BboxTransformTo, TransformedBbox) +from matplotlib import _warn_non_gui_show docstring.interpd.update(projection_names = get_projection_names()) @@ -328,6 +329,19 @@ def __init__(self, self.clf() self._cachedRenderer = None + def show(self): + """ + If using a GUI backend, display the figure window. + + For non-GUI backends, this does nothing. + """ + if hasattr(self, 'canvas'): + manager = getattr(self.canvas, 'manager', None) + if manager is not None: + manager.show() + return + _warn_non_gui_show() + def _get_axes(self): return self._axstack.as_list()