From 7a9c7d4e4ebae4d095c446fd9f79e0be62b1bdc6 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 20 Aug 2012 10:59:48 -0400 Subject: [PATCH 1/2] Add a "show" method to figures that don't have a GUI backend. Fixed #835. --- lib/matplotlib/backend_bases.py | 12 +++++++++--- lib/matplotlib/figure.py | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index c0eeb0543db6..c860f3686057 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2423,14 +2423,20 @@ 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. + """ + pass + def destroy(self): pass diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 2dafa2c5827b..d14a7b442361 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -328,6 +328,20 @@ 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. + """ + manager = getattr(self.canvas, 'manager') + if manager is not None: + manager.show() + import warnings + warnings.warn( + "matplotlib is currently using a non-GUI backend, " + "so can not show the figure") + def _get_axes(self): return self._axstack.as_list() From 58425248e38956a9aa276e1913d23476d469c5ec Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 28 Aug 2012 11:18:24 -0400 Subject: [PATCH 2/2] Warn when calling "show" in a non-GUI backend, and provide a convenient way to suppress the warning. --- lib/matplotlib/__init__.py | 18 ++++++++++++++++++ lib/matplotlib/backend_bases.py | 3 ++- lib/matplotlib/figure.py | 14 +++++++------- 3 files changed, 27 insertions(+), 8 deletions(-) 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 c860f3686057..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 @@ -2435,7 +2436,7 @@ def show(self): """ For GUI backends, show the figure window and redraw. """ - pass + _warn_non_gui_show() def destroy(self): pass diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index d14a7b442361..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()) @@ -334,13 +335,12 @@ def show(self): For non-GUI backends, this does nothing. """ - manager = getattr(self.canvas, 'manager') - if manager is not None: - manager.show() - import warnings - warnings.warn( - "matplotlib is currently using a non-GUI backend, " - "so can not show the figure") + 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()