@@ -445,29 +445,34 @@ 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 )
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 ())
460461
461462 def clear (self , figure ):
462463 """Reset the axes stack"""
463464 if figure in self .views :
464465 self .views [figure ].clear ()
465466 self .positions [figure ].clear ()
467+ self .home_views [figure ].clear ()
468+ self .update_home_views ()
466469
467470 def update_view (self ):
468471 """
469- Update the viewlim and position from the view and
470- position stack for each axes
472+ Update the view limits and position for each axes from the current
473+ stack position. If any axes are present in the figure that aren't in
474+ the current stack position, use the home view limits for those axes and
475+ don't update *any* positions.
471476 """
472477
473478 views = self .views [self .figure ]()
@@ -476,28 +481,64 @@ def update_view(self):
476481 pos = self .positions [self .figure ]()
477482 if pos is None :
478483 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' )
484+ home_views = self .home_views [self .figure ]
485+ all_axes = self .figure .get_axes ()
486+ for a in all_axes :
487+ if a in views :
488+ cur_view = views [a ]
489+ else :
490+ cur_view = home_views [a ]
491+ a ._set_view (cur_view )
492+
493+ if set (all_axes ).issubset (pos .keys ()):
494+ for a in all_axes :
495+ # Restore both the original and modified positions
496+ a .set_position (pos [a ][0 ], 'original' )
497+ a .set_position (pos [a ][1 ], 'active' )
484498
485499 self .figure .canvas .draw_idle ()
486500
487501 def push_current (self ):
488- """push the current view limits and position onto the stack"""
502+ """
503+ Push the current view limits and position onto their respective stacks
504+ """
489505
490- views = []
491- pos = []
506+ views = WeakKeyDictionary ()
507+ pos = WeakKeyDictionary ()
492508 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 ()))
509+ views [a ] = a ._get_view ()
510+ pos [a ] = self ._axes_pos (a )
498511 self .views [self .figure ].push (views )
499512 self .positions [self .figure ].push (pos )
500513
514+ def _axes_pos (self , ax ):
515+ """
516+ Return the original and modified positions for the specified axes
517+
518+ Parameters
519+ ----------
520+ ax : (matplotlib.axes.AxesSubplot)
521+ The axes to get the positions for
522+
523+ Returns
524+ -------
525+ limits : (tuple)
526+ A tuple of the original and modified positions
527+ """
528+
529+ return (ax .get_position (True ).frozen (),
530+ ax .get_position ().frozen ())
531+
532+ def update_home_views (self ):
533+ """
534+ Make sure that self.home_views has an entry for all axes present in the
535+ figure
536+ """
537+
538+ for a in self .figure .get_axes ():
539+ if a not in self .home_views [self .figure ]:
540+ self .home_views [self .figure ][a ] = a ._get_view ()
541+
501542 def refresh_locators (self ):
502543 """Redraw the canvases, update the locators"""
503544 for a in self .figure .get_axes ():
0 commit comments