Skip to content

Commit cafe668

Browse files
committed
The views positions mixin automatically adds the clear as axobserver
1 parent 2c9a195 commit cafe668

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

examples/user_interfaces/navigation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import matplotlib
2-
matplotlib.use('GTK3Cairo')
3-
# matplotlib.use('TkAGG')
2+
# matplotlib.use('GTK3Cairo')
3+
matplotlib.use('TkAGG')
44
matplotlib.rcParams['toolbar'] = 'navigation'
55
import matplotlib.pyplot as plt
66
from matplotlib.backend_tools import ToolBase
@@ -54,6 +54,6 @@ def trigger(self, event):
5454
fig.canvas.manager.navigation.add_tool('copy', CopyToolGTK3)
5555

5656
# Just for fun, lets remove the back button
57-
fig.canvas.manager.navigation.remove_tool('Back')
57+
# fig.canvas.manager.navigation.remove_tool('Back')
5858

5959
plt.show()

lib/matplotlib/backend_tools.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ def set_figure(self, figure):
9797

9898

9999
class ToolPersistentBase(ToolBase):
100-
"""Persisten tool
100+
"""Persistent tool
101101
102-
Persistent Tools are keept alive after their initialization,
102+
Persistent Tools are kept alive after their initialization,
103103
a reference of the instance is kept by `navigation`.
104104
105105
Notes
@@ -287,20 +287,41 @@ def trigger(self, event):
287287

288288

289289
class ViewsPositionsMixin(object):
290+
"""Mixin to handle changes in views and positions
291+
292+
Tools that change the views and positions, use this mixin to
293+
keep track of the changes.
294+
"""
295+
290296
views = WeakKeyDictionary()
297+
"""Record of views with Figure objects as keys"""
298+
291299
positions = WeakKeyDictionary()
300+
"""Record of positions with Figure objects as keys"""
292301

293302
def init_vp(self):
303+
"""Add a figure to the list of figures handled by this mixin
304+
305+
To handle the views and positions for a given figure, this method
306+
has to be called at least once before any other method.
307+
308+
The best way to call it is during the set_figure method of the tools
309+
"""
294310
if self.figure not in self.views:
295311
self.views[self.figure] = cbook.Stack()
296312
self.positions[self.figure] = cbook.Stack()
297313
# Define Home
298314
self.push_current()
315+
# Adding the clear method as axobserver, removes this burden from
316+
# the backend
317+
self.figure.add_axobserver(self.clear)
299318

300319
@classmethod
301320
def clear(cls, figure):
302321
"""Reset the axes stack"""
322+
print('clear')
303323
if figure in cls.views:
324+
print('done clear')
304325
cls.views[figure].clear()
305326
cls.positions[figure].clear()
306327

@@ -375,12 +396,9 @@ def forward(self):
375396
self.positions[self.figure].forward()
376397

377398

378-
def clear_views_positions(figure):
379-
ViewsPositionsMixin.clear(figure)
380-
381-
382399
class ViewsPositionsBase(ViewsPositionsMixin, ToolBase):
383400
# Simple base to avoid repeating code on Home, Back and Forward
401+
# Not of much use for other tools, so not documented
384402
_on_trigger = None
385403

386404
def set_figure(self, *args):
@@ -435,6 +453,8 @@ class SaveFigureBase(ToolBase):
435453

436454

437455
class ZoomPanBase(ViewsPositionsMixin, ToolToggleBase):
456+
# Base class to group common functionality between zoom and pan
457+
# Not of much use for other tools, so not documented
438458
def __init__(self, *args):
439459
ToolToggleBase.__init__(self, *args)
440460
self.init_vp()

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ def fn_name(): return sys._getframe(1).f_code.co_name
3131
from matplotlib.backend_bases import RendererBase, GraphicsContextBase, \
3232
FigureManagerBase, FigureCanvasBase, NavigationToolbar2, cursors, TimerBase
3333
from matplotlib.backend_bases import ShowBase, ToolbarBase, NavigationBase
34-
from matplotlib.backend_tools import SaveFigureBase, ConfigureSubplotsBase, \
35-
clear_views_positions
34+
from matplotlib.backend_tools import SaveFigureBase, ConfigureSubplotsBase
3635

3736
from matplotlib.cbook import is_string_like, is_writable_file_like
3837
from matplotlib.colors import colorConverter
@@ -442,7 +441,7 @@ def destroy(*args):
442441
def notify_axes_change(fig):
443442
'this will be called whenever the current axes is changed'
444443
if self.navigation is not None:
445-
clear_views_positions(fig)
444+
pass
446445
elif self.toolbar is not None: self.toolbar.update()
447446
self.canvas.figure.add_axobserver(notify_axes_change)
448447

lib/matplotlib/backends/backend_tkagg.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
from matplotlib.backend_bases import FigureManagerBase, FigureCanvasBase
2222
from matplotlib.backend_bases import NavigationToolbar2, cursors, TimerBase
2323
from matplotlib.backend_bases import ShowBase, ToolbarBase, NavigationBase
24-
from matplotlib.backend_tools import SaveFigureBase, ConfigureSubplotsBase, \
25-
clear_views_positions
24+
from matplotlib.backend_tools import SaveFigureBase, ConfigureSubplotsBase
2625
from matplotlib._pylab_helpers import Gcf
2726

2827
from matplotlib.figure import Figure
@@ -544,7 +543,7 @@ def __init__(self, canvas, num, window):
544543
def notify_axes_change(fig):
545544
'this will be called whenever the current axes is changed'
546545
if self.navigation is not None:
547-
clear_views_positions(fig)
546+
pass
548547
elif self.toolbar is not None:
549548
self.toolbar.update()
550549
self.canvas.figure.add_axobserver(notify_axes_change)

0 commit comments

Comments
 (0)