From 55dd1494f60af6c23cfd0924a41ab303f495d76a Mon Sep 17 00:00:00 2001 From: Ocean Wolf Date: Mon, 28 Jul 2014 19:40:38 +0200 Subject: [PATCH 1/5] Small refactor so that we first initiate the Navigation (ToolManager), before filling it with tools. Added a nice utility API function, Navigation.addTools. --- lib/matplotlib/backend_bases.py | 23 ++++++++++++++++------- lib/matplotlib/pyplot.py | 4 ++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 0f47c4653e1a..567ff03b2073 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -3220,13 +3220,6 @@ def __init__(self, manager): # to write into toolbar message self.messagelock = widgets.LockDraw() - for name, tool in tools.tools: - if tool is None: - if self.toolbar is not None: - self.toolbar.add_separator(-1) - else: - self.add_tool(name, tool, None) - self._last_cursor = self._default_cursor @property @@ -3329,6 +3322,22 @@ def remove_tool(self, name): if self.toolbar: self.toolbar._remove_toolitem(name) + def add_tools(self, tools): + """ Add multiple tools to `Navigation` + + Parameters + ---------- + tools : a list of tuples which contains the id of the tool and + a either a reference to the tool Tool class itself, or None to + insert a spacer. See @add_tool. + """ + for name, tool in tools: + if tool is None: + if self.toolbar is not None: + self.toolbar.add_separator(-1) + else: + self.add_tool(name, tool, None) + def add_tool(self, name, tool, position=None): """Add tool to `Navigation` diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index da553fe212c6..7ca2ef8eef65 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -31,6 +31,7 @@ from matplotlib.cbook import _string_to_bool from matplotlib import docstring from matplotlib.backend_bases import FigureCanvasBase +from matplotlib.backend_tools import tools as default_tools from matplotlib.figure import Figure, figaspect from matplotlib.gridspec import GridSpec from matplotlib.image import imread as _imread @@ -428,6 +429,9 @@ def figure(num=None, # autoincrement if None, else integer from 1-N FigureClass=FigureClass, **kwargs) + if rcParams['toolbar'] == 'navigation': + figManager.navigation.add_tools(default_tools) + if figLabel: figManager.set_window_title(figLabel) figManager.canvas.figure.set_label(figLabel) From 15ac091aab13a149c971ee212339d3f1e3f0d05d Mon Sep 17 00:00:00 2001 From: Ocean Wolf Date: Mon, 28 Jul 2014 23:57:39 +0200 Subject: [PATCH 2/5] Update for Sphinx documentation --- lib/matplotlib/backend_bases.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 567ff03b2073..96adc3160155 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -3329,7 +3329,7 @@ def add_tools(self, tools): ---------- tools : a list of tuples which contains the id of the tool and a either a reference to the tool Tool class itself, or None to - insert a spacer. See @add_tool. + insert a spacer. See :func:`add_tool`. """ for name, tool in tools: if tool is None: From 8cd241ce04bfb6f9e8af7c70dc33eb6f4dc7b7db Mon Sep 17 00:00:00 2001 From: Ocean Wolf Date: Tue, 29 Jul 2014 16:06:38 +0200 Subject: [PATCH 3/5] Moved default_tool initilisation to FigureManagerBase and cleaned. --- lib/matplotlib/backend_bases.py | 9 +++++++++ lib/matplotlib/backends/backend_gtk3.py | 3 --- lib/matplotlib/backends/backend_tkagg.py | 6 +----- lib/matplotlib/pyplot.py | 4 ---- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 96adc3160155..d49abd5bd2ab 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2556,6 +2556,10 @@ def __init__(self, canvas, num): """ + self.toolbar = self._get_toolbar() + self.navigation = self._get_navigation() + self.navigation.add_tools(tools.tools) + def show(self): """ For GUI backends, show the figure window and redraw. @@ -2603,6 +2607,11 @@ def set_window_title(self, title): """ pass + def _get_toolbar(self): + return None + + def _get_navigation(self): + return None cursors = tools.cursors diff --git a/lib/matplotlib/backends/backend_gtk3.py b/lib/matplotlib/backends/backend_gtk3.py index dab819fa9193..8559527c44b5 100644 --- a/lib/matplotlib/backends/backend_gtk3.py +++ b/lib/matplotlib/backends/backend_gtk3.py @@ -413,9 +413,6 @@ def __init__(self, canvas, num): self.vbox.pack_start(self.canvas, True, True, 0) - self.toolbar = self._get_toolbar() - self.navigation = self._get_navigation() - # calculate size for window w = int (self.canvas.figure.bbox.width) h = int (self.canvas.figure.bbox.height) diff --git a/lib/matplotlib/backends/backend_tkagg.py b/lib/matplotlib/backends/backend_tkagg.py index a17c0f4c8c13..2310b9ac2479 100644 --- a/lib/matplotlib/backends/backend_tkagg.py +++ b/lib/matplotlib/backends/backend_tkagg.py @@ -526,19 +526,15 @@ class FigureManagerTkAgg(FigureManagerBase): window : The tk.Window """ def __init__(self, canvas, num, window): - FigureManagerBase.__init__(self, canvas, num) self.window = window + FigureManagerBase.__init__(self, canvas, num) self.window.withdraw() self.set_window_title("Figure %d" % num) - self.canvas = canvas self._num = num _, _, w, h = canvas.figure.bbox.bounds w, h = int(w), int(h) self.window.minsize(int(w*3/4),int(h*3/4)) - self.toolbar = self._get_toolbar() - self.navigation = self._get_navigation() - if self.toolbar is not None: self.toolbar.update() self.canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 7ca2ef8eef65..da553fe212c6 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -31,7 +31,6 @@ from matplotlib.cbook import _string_to_bool from matplotlib import docstring from matplotlib.backend_bases import FigureCanvasBase -from matplotlib.backend_tools import tools as default_tools from matplotlib.figure import Figure, figaspect from matplotlib.gridspec import GridSpec from matplotlib.image import imread as _imread @@ -429,9 +428,6 @@ def figure(num=None, # autoincrement if None, else integer from 1-N FigureClass=FigureClass, **kwargs) - if rcParams['toolbar'] == 'navigation': - figManager.navigation.add_tools(default_tools) - if figLabel: figManager.set_window_title(figLabel) figManager.canvas.figure.set_label(figLabel) From 39f5b749a597c8457ccb6d930c19a449f7aa92a6 Mon Sep 17 00:00:00 2001 From: Ocean Wolf Date: Tue, 29 Jul 2014 16:47:16 +0200 Subject: [PATCH 4/5] Fix navigation --- lib/matplotlib/backend_bases.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index d49abd5bd2ab..19e3a15151b2 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2558,7 +2558,8 @@ def __init__(self, canvas, num): self.toolbar = self._get_toolbar() self.navigation = self._get_navigation() - self.navigation.add_tools(tools.tools) + if rcParams['toolbar'] == 'navigation': + self.navigation.add_tools(tools.tools) def show(self): """ From b20daded90e0134d5af51c7fdbafae585a80a114 Mon Sep 17 00:00:00 2001 From: Ocean Wolf Date: Tue, 29 Jul 2014 19:10:55 +0200 Subject: [PATCH 5/5] Temporary fix to backends --- lib/matplotlib/backend_bases.py | 10 ---------- lib/matplotlib/backends/backend_gtk3.py | 7 ++++++- lib/matplotlib/backends/backend_tkagg.py | 10 ++++++++-- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 19e3a15151b2..96adc3160155 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2556,11 +2556,6 @@ def __init__(self, canvas, num): """ - self.toolbar = self._get_toolbar() - self.navigation = self._get_navigation() - if rcParams['toolbar'] == 'navigation': - self.navigation.add_tools(tools.tools) - def show(self): """ For GUI backends, show the figure window and redraw. @@ -2608,11 +2603,6 @@ def set_window_title(self, title): """ pass - def _get_toolbar(self): - return None - - def _get_navigation(self): - return None cursors = tools.cursors diff --git a/lib/matplotlib/backends/backend_gtk3.py b/lib/matplotlib/backends/backend_gtk3.py index 8559527c44b5..df222fa2a520 100644 --- a/lib/matplotlib/backends/backend_gtk3.py +++ b/lib/matplotlib/backends/backend_gtk3.py @@ -31,7 +31,7 @@ def fn_name(): return sys._getframe(1).f_code.co_name from matplotlib.backend_bases import RendererBase, GraphicsContextBase, \ FigureManagerBase, FigureCanvasBase, NavigationToolbar2, cursors, TimerBase from matplotlib.backend_bases import ShowBase, ToolbarBase, NavigationBase -from matplotlib.backend_tools import SaveFigureBase, ConfigureSubplotsBase +from matplotlib.backend_tools import SaveFigureBase, ConfigureSubplotsBase, tools from matplotlib.cbook import is_string_like, is_writable_file_like from matplotlib.colors import colorConverter @@ -413,6 +413,11 @@ def __init__(self, canvas, num): self.vbox.pack_start(self.canvas, True, True, 0) + self.toolbar = self._get_toolbar() + self.navigation = self._get_navigation() + if matplotlib.rcParams['toolbar'] == 'navigation': + self.navigation.add_tools(tools) + # calculate size for window w = int (self.canvas.figure.bbox.width) h = int (self.canvas.figure.bbox.height) diff --git a/lib/matplotlib/backends/backend_tkagg.py b/lib/matplotlib/backends/backend_tkagg.py index 2310b9ac2479..bbaf35670cf0 100644 --- a/lib/matplotlib/backends/backend_tkagg.py +++ b/lib/matplotlib/backends/backend_tkagg.py @@ -21,7 +21,7 @@ from matplotlib.backend_bases import FigureManagerBase, FigureCanvasBase from matplotlib.backend_bases import NavigationToolbar2, cursors, TimerBase from matplotlib.backend_bases import ShowBase, ToolbarBase, NavigationBase -from matplotlib.backend_tools import SaveFigureBase, ConfigureSubplotsBase +from matplotlib.backend_tools import SaveFigureBase, ConfigureSubplotsBase, tools from matplotlib._pylab_helpers import Gcf from matplotlib.figure import Figure @@ -526,15 +526,21 @@ class FigureManagerTkAgg(FigureManagerBase): window : The tk.Window """ def __init__(self, canvas, num, window): - self.window = window FigureManagerBase.__init__(self, canvas, num) + self.window = window self.window.withdraw() self.set_window_title("Figure %d" % num) + self.canvas = canvas self._num = num _, _, w, h = canvas.figure.bbox.bounds w, h = int(w), int(h) self.window.minsize(int(w*3/4),int(h*3/4)) + self.toolbar = self._get_toolbar() + self.navigation = self._get_navigation() + if matplotlib.rcParams['toolbar'] == 'navigation': + self.navigation.add_tools(tools) + if self.toolbar is not None: self.toolbar.update() self.canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)