Skip to content

Commit 94c711e

Browse files
OceanWolffariza
authored andcommitted
Small refactor so that we first initiate the Navigation (ToolManager), before filling it with tools.
Added a nice utility API function, Navigation.addTools.
1 parent 224f745 commit 94c711e

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3262,13 +3262,6 @@ def __init__(self, manager):
32623262
# to write into toolbar message
32633263
self.messagelock = widgets.LockDraw()
32643264

3265-
for name, tool in tools.tools:
3266-
if tool is None:
3267-
if self.toolbar is not None:
3268-
self.toolbar.add_separator(-1)
3269-
else:
3270-
self.add_tool(name, tool, None)
3271-
32723265
self._last_cursor = self._default_cursor
32733266

32743267
@property
@@ -3371,6 +3364,22 @@ def remove_tool(self, name):
33713364
if self.toolbar:
33723365
self.toolbar._remove_toolitem(name)
33733366

3367+
def add_tools(self, tools):
3368+
""" Add multiple tools to `Navigation`
3369+
3370+
Parameters
3371+
----------
3372+
tools : a list of tuples which contains the id of the tool and
3373+
a either a reference to the tool Tool class itself, or None to
3374+
insert a spacer. See :func:`add_tool`.
3375+
"""
3376+
for name, tool in tools:
3377+
if tool is None:
3378+
if self.toolbar is not None:
3379+
self.toolbar.add_separator(-1)
3380+
else:
3381+
self.add_tool(name, tool, None)
3382+
33743383
def add_tool(self, name, tool, position=None):
33753384
"""Add tool to `Navigation`
33763385

lib/matplotlib/pyplot.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from matplotlib.cbook import _string_to_bool
3232
from matplotlib import docstring
3333
from matplotlib.backend_bases import FigureCanvasBase
34+
from matplotlib.backend_tools import tools as default_tools
3435
from matplotlib.figure import Figure, figaspect
3536
from matplotlib.gridspec import GridSpec
3637
from matplotlib.image import imread as _imread
@@ -433,6 +434,9 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
433434
FigureClass=FigureClass,
434435
**kwargs)
435436

437+
if rcParams['toolbar'] == 'navigation':
438+
figManager.navigation.add_tools(default_tools)
439+
436440
if figLabel:
437441
figManager.set_window_title(figLabel)
438442
figManager.canvas.figure.set_label(figLabel)

0 commit comments

Comments
 (0)