@@ -2775,9 +2775,7 @@ class NavigationToolbar2(object):
2775
2775
def __init__ (self , canvas ):
2776
2776
self .canvas = canvas
2777
2777
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 ()
2781
2779
self ._xypress = None # the location and axis info at the time
2782
2780
# of the press
2783
2781
self ._idPress = None
@@ -2799,19 +2797,24 @@ def __init__(self, canvas):
2799
2797
self .set_history_buttons ()
2800
2798
2801
2799
@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 ()
2807
2811
2808
2812
def set_message (self , s ):
2809
2813
"""Display a message on toolbar or in status bar."""
2810
2814
2811
2815
def back (self , * args ):
2812
2816
"""move back up the view lim stack"""
2813
- self ._views .back ()
2814
- self ._positions .back ()
2817
+ self ._nav_stack .back ()
2815
2818
self .set_history_buttons ()
2816
2819
self ._update_view ()
2817
2820
@@ -2830,15 +2833,13 @@ def remove_rubberband(self):
2830
2833
2831
2834
def forward (self , * args ):
2832
2835
"""Move forward in the view lim stack."""
2833
- self ._views .forward ()
2834
- self ._positions .forward ()
2836
+ self ._nav_stack .forward ()
2835
2837
self .set_history_buttons ()
2836
2838
self ._update_view ()
2837
2839
2838
2840
def home (self , * args ):
2839
2841
"""Restore the original view."""
2840
- self ._views .home ()
2841
- self ._positions .home ()
2842
+ self ._nav_stack .home ()
2842
2843
self .set_history_buttons ()
2843
2844
self ._update_view ()
2844
2845
@@ -3015,16 +3016,13 @@ def _switch_off_zoom_mode(self, event):
3015
3016
3016
3017
def push_current (self ):
3017
3018
"""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 ))
3028
3026
self .set_history_buttons ()
3029
3027
3030
3028
def release (self , event ):
@@ -3146,18 +3144,15 @@ def _update_view(self):
3146
3144
position stack for each axes.
3147
3145
"""
3148
3146
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 :
3154
3149
return
3150
+ axs , views , pos = nav_info
3155
3151
for i , a in enumerate (self .canvas .figure .get_axes ()):
3156
3152
a ._set_view (views [i ])
3157
3153
# Restore both the original and modified positions
3158
3154
a .set_position (pos [i ][0 ], 'original' )
3159
3155
a .set_position (pos [i ][1 ], 'active' )
3160
-
3161
3156
self .canvas .draw_idle ()
3162
3157
3163
3158
def save_figure (self , * args ):
@@ -3175,8 +3170,7 @@ def set_cursor(self, cursor):
3175
3170
3176
3171
def update (self ):
3177
3172
"""Reset the axes stack."""
3178
- self ._views .clear ()
3179
- self ._positions .clear ()
3173
+ self ._nav_stack .clear ()
3180
3174
self .set_history_buttons ()
3181
3175
3182
3176
def zoom (self , * args ):
0 commit comments