Skip to content

Commit 92a6323

Browse files
committed
Toolbars keep history if axes change
1 parent 634c4bf commit 92a6323

File tree

1 file changed

+57
-24
lines changed

1 file changed

+57
-24
lines changed

lib/matplotlib/backend_tools.py

+57-24
Original file line numberDiff line numberDiff line change
@@ -445,29 +445,26 @@ class ToolViewsPositions(ToolBase):
445445
def __init__(self, *args, **kwargs):
446446
self.views = WeakKeyDictionary()
447447
self.positions = WeakKeyDictionary()
448+
self.home_views = WeakKeyDictionary()
448449
ToolBase.__init__(self, *args, **kwargs)
449450

450451
def add_figure(self):
451452
"""Add the current figure to the stack of views and positions"""
452453
if self.figure not in self.views:
453454
self.views[self.figure] = cbook.Stack()
454455
self.positions[self.figure] = cbook.Stack()
456+
self.home_views[self.figure] = WeakKeyDictionary()
455457
# Define Home
456458
self.push_current()
457-
# Adding the clear method as axobserver, removes this burden from
458-
# the backend
459-
self.figure.add_axobserver(self.clear)
460-
461-
def clear(self, figure):
462-
"""Reset the axes stack"""
463-
if figure in self.views:
464-
self.views[figure].clear()
465-
self.positions[figure].clear()
459+
# Make sure we add a home view for new axes as they're added
460+
self.figure.add_axobserver(lambda fig: self.update_home_views())
466461

467462
def update_view(self):
468463
"""
469-
Update the viewlim and position from the view and
470-
position stack for each axes
464+
Update the view limits and position for each axes from the current
465+
stack position. If any axes are present in the figure that aren't in
466+
the current stack position, use the home view limits for those axes and
467+
don't update *any* positions.
471468
"""
472469

473470
views = self.views[self.figure]()
@@ -476,28 +473,64 @@ def update_view(self):
476473
pos = self.positions[self.figure]()
477474
if pos is None:
478475
return
479-
for i, a in enumerate(self.figure.get_axes()):
480-
a._set_view(views[i])
481-
# Restore both the original and modified positions
482-
a.set_position(pos[i][0], 'original')
483-
a.set_position(pos[i][1], 'active')
476+
home_views = self.home_views[self.figure]
477+
all_axes = self.figure.get_axes()
478+
for a in all_axes:
479+
if a in views:
480+
cur_view = views[a]
481+
else:
482+
cur_view = home_views[a]
483+
a._set_view(cur_view)
484+
485+
if set(all_axes).issubset(pos.keys()):
486+
for a in all_axes:
487+
# Restore both the original and modified positions
488+
a.set_position(pos[a][0], 'original')
489+
a.set_position(pos[a][1], 'active')
484490

485491
self.figure.canvas.draw_idle()
486492

487493
def push_current(self):
488-
"""push the current view limits and position onto the stack"""
494+
"""
495+
Push the current view limits and position onto their respective stacks
496+
"""
489497

490-
views = []
491-
pos = []
498+
views = WeakKeyDictionary()
499+
pos = WeakKeyDictionary()
492500
for a in self.figure.get_axes():
493-
views.append(a._get_view())
494-
# Store both the original and modified positions
495-
pos.append((
496-
a.get_position(True).frozen(),
497-
a.get_position().frozen()))
501+
views[a] = a._get_view()
502+
pos[a] = self._axes_pos(a)
498503
self.views[self.figure].push(views)
499504
self.positions[self.figure].push(pos)
500505

506+
def _axes_pos(self, ax):
507+
"""
508+
Return the original and modified positions for the specified axes
509+
510+
Parameters
511+
----------
512+
ax : (matplotlib.axes.AxesSubplot)
513+
The axes to get the positions for
514+
515+
Returns
516+
-------
517+
limits : (tuple)
518+
A tuple of the original and modified positions
519+
"""
520+
521+
return (ax.get_position(True).frozen(),
522+
ax.get_position().frozen())
523+
524+
def update_home_views(self):
525+
"""
526+
Make sure that self.home_views has an entry for all axes present in the
527+
figure
528+
"""
529+
530+
for a in self.figure.get_axes():
531+
if a not in self.home_views[self.figure]:
532+
self.home_views[self.figure][a] = a._get_view()
533+
501534
def refresh_locators(self):
502535
"""Redraw the canvases, update the locators"""
503536
for a in self.figure.get_axes():

0 commit comments

Comments
 (0)