Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c849398

Browse files
committed
Merge pull request #4857 from KevKeating/iss2511-cmb
Toolbars keep history if axes change (navtoolbar2 + toolmanager)
2 parents 7646324 + b5506dc commit c849398

1 file changed

Lines changed: 59 additions & 18 deletions

File tree

lib/matplotlib/backend_tools.py

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -455,29 +455,34 @@ class ToolViewsPositions(ToolBase):
455455
def __init__(self, *args, **kwargs):
456456
self.views = WeakKeyDictionary()
457457
self.positions = WeakKeyDictionary()
458+
self.home_views = WeakKeyDictionary()
458459
ToolBase.__init__(self, *args, **kwargs)
459460

460461
def add_figure(self):
461462
"""Add the current figure to the stack of views and positions"""
462463
if self.figure not in self.views:
463464
self.views[self.figure] = cbook.Stack()
464465
self.positions[self.figure] = cbook.Stack()
466+
self.home_views[self.figure] = WeakKeyDictionary()
465467
# Define Home
466468
self.push_current()
467-
# Adding the clear method as axobserver, removes this burden from
468-
# the backend
469-
self.figure.add_axobserver(self.clear)
469+
# Make sure we add a home view for new axes as they're added
470+
self.figure.add_axobserver(lambda fig: self.update_home_views())
470471

471472
def clear(self, figure):
472473
"""Reset the axes stack"""
473474
if figure in self.views:
474475
self.views[figure].clear()
475476
self.positions[figure].clear()
477+
self.home_views[figure].clear()
478+
self.update_home_views()
476479

477480
def update_view(self):
478481
"""
479-
Update the viewlim and position from the view and
480-
position stack for each axes
482+
Update the view limits and position for each axes from the current
483+
stack position. If any axes are present in the figure that aren't in
484+
the current stack position, use the home view limits for those axes and
485+
don't update *any* positions.
481486
"""
482487

483488
views = self.views[self.figure]()
@@ -486,28 +491,64 @@ def update_view(self):
486491
pos = self.positions[self.figure]()
487492
if pos is None:
488493
return
489-
for i, a in enumerate(self.figure.get_axes()):
490-
a._set_view(views[i])
491-
# Restore both the original and modified positions
492-
a.set_position(pos[i][0], 'original')
493-
a.set_position(pos[i][1], 'active')
494+
home_views = self.home_views[self.figure]
495+
all_axes = self.figure.get_axes()
496+
for a in all_axes:
497+
if a in views:
498+
cur_view = views[a]
499+
else:
500+
cur_view = home_views[a]
501+
a._set_view(cur_view)
502+
503+
if set(all_axes).issubset(pos.keys()):
504+
for a in all_axes:
505+
# Restore both the original and modified positions
506+
a.set_position(pos[a][0], 'original')
507+
a.set_position(pos[a][1], 'active')
494508

495509
self.figure.canvas.draw_idle()
496510

497511
def push_current(self):
498-
"""push the current view limits and position onto the stack"""
512+
"""
513+
Push the current view limits and position onto their respective stacks
514+
"""
499515

500-
views = []
501-
pos = []
516+
views = WeakKeyDictionary()
517+
pos = WeakKeyDictionary()
502518
for a in self.figure.get_axes():
503-
views.append(a._get_view())
504-
# Store both the original and modified positions
505-
pos.append((
506-
a.get_position(True).frozen(),
507-
a.get_position().frozen()))
519+
views[a] = a._get_view()
520+
pos[a] = self._axes_pos(a)
508521
self.views[self.figure].push(views)
509522
self.positions[self.figure].push(pos)
510523

524+
def _axes_pos(self, ax):
525+
"""
526+
Return the original and modified positions for the specified axes
527+
528+
Parameters
529+
----------
530+
ax : (matplotlib.axes.AxesSubplot)
531+
The axes to get the positions for
532+
533+
Returns
534+
-------
535+
limits : (tuple)
536+
A tuple of the original and modified positions
537+
"""
538+
539+
return (ax.get_position(True).frozen(),
540+
ax.get_position().frozen())
541+
542+
def update_home_views(self):
543+
"""
544+
Make sure that self.home_views has an entry for all axes present in the
545+
figure
546+
"""
547+
548+
for a in self.figure.get_axes():
549+
if a not in self.home_views[self.figure]:
550+
self.home_views[self.figure][a] = a._get_view()
551+
511552
def refresh_locators(self):
512553
"""Redraw the canvases, update the locators"""
513554
for a in self.figure.get_axes():

0 commit comments

Comments
 (0)