@@ -2775,9 +2775,7 @@ class NavigationToolbar2(object):
27752775 def __init__ (self , canvas ):
27762776 self .canvas = canvas
27772777 canvas .toolbar = self
2778- # a dict from axes index to a list of view limits
2779- self ._views = cbook .Stack ()
2780- self ._positions = cbook .Stack () # stack of subplot positions
2778+ self ._nav_stack = cbook .Stack ()
27812779 self ._xypress = None # the location and axis info at the time
27822780 # of the press
27832781 self ._idPress = None
@@ -2799,19 +2797,24 @@ def __init__(self, canvas):
27992797 self .set_history_buttons ()
28002798
28012799 @partial (canvas .mpl_connect , 'draw_event' )
2802- def define_home (event ):
2803- self .push_current ()
2804- # The decorator sets `define_home` to the callback cid, so we can
2805- # disconnect it after the first use.
2806- canvas .mpl_disconnect (define_home )
2800+ def update_stack (event ):
2801+ nav_info = self ._nav_stack ()
2802+ if nav_info is None :
2803+ # Define the true initial navigation info.
2804+ self .push_current ()
2805+ else :
2806+ axes , views , positions = nav_info
2807+ if axes != self .canvas .figure .axes :
2808+ # An axes has been added or removed, so update the
2809+ # navigation info too.
2810+ self .push_current ()
28072811
28082812 def set_message (self , s ):
28092813 """Display a message on toolbar or in status bar."""
28102814
28112815 def back (self , * args ):
28122816 """move back up the view lim stack"""
2813- self ._views .back ()
2814- self ._positions .back ()
2817+ self ._nav_stack .back ()
28152818 self .set_history_buttons ()
28162819 self ._update_view ()
28172820
@@ -2830,15 +2833,13 @@ def remove_rubberband(self):
28302833
28312834 def forward (self , * args ):
28322835 """Move forward in the view lim stack."""
2833- self ._views .forward ()
2834- self ._positions .forward ()
2836+ self ._nav_stack .forward ()
28352837 self .set_history_buttons ()
28362838 self ._update_view ()
28372839
28382840 def home (self , * args ):
28392841 """Restore the original view."""
2840- self ._views .home ()
2841- self ._positions .home ()
2842+ self ._nav_stack .home ()
28422843 self .set_history_buttons ()
28432844 self ._update_view ()
28442845
@@ -3015,16 +3016,13 @@ def _switch_off_zoom_mode(self, event):
30153016
30163017 def push_current (self ):
30173018 """Push the current view limits and position onto the stack."""
3018- views = []
3019- pos = []
3020- for a in self .canvas .figure .get_axes ():
3021- views .append (a ._get_view ())
3022- # Store both the original and modified positions
3023- pos .append ((
3024- a .get_position (True ).frozen (),
3025- a .get_position ().frozen ()))
3026- self ._views .push (views )
3027- self ._positions .push (pos )
3019+ axs = self .canvas .figure .axes
3020+ views = [ax ._get_view () for ax in axs ]
3021+ # Store both the original and modified positions.
3022+ positions = [
3023+ (ax .get_position (True ).frozen (), ax .get_position ().frozen ())
3024+ for ax in axs ]
3025+ self ._nav_stack .push ((axs , views , positions ))
30283026 self .set_history_buttons ()
30293027
30303028 def release (self , event ):
@@ -3146,18 +3144,15 @@ def _update_view(self):
31463144 position stack for each axes.
31473145 """
31483146
3149- views = self ._views ()
3150- if views is None :
3151- return
3152- pos = self ._positions ()
3153- if pos is None :
3147+ nav_info = self ._nav_stack ()
3148+ if nav_info is None :
31543149 return
3150+ axs , views , pos = nav_info
31553151 for i , a in enumerate (self .canvas .figure .get_axes ()):
31563152 a ._set_view (views [i ])
31573153 # Restore both the original and modified positions
31583154 a .set_position (pos [i ][0 ], 'original' )
31593155 a .set_position (pos [i ][1 ], 'active' )
3160-
31613156 self .canvas .draw_idle ()
31623157
31633158 def save_figure (self , * args ):
@@ -3175,8 +3170,7 @@ def set_cursor(self, cursor):
31753170
31763171 def update (self ):
31773172 """Reset the axes stack."""
3178- self ._views .clear ()
3179- self ._positions .clear ()
3173+ self ._nav_stack .clear ()
31803174 self .set_history_buttons ()
31813175
31823176 def zoom (self , * args ):
0 commit comments