@@ -445,29 +445,26 @@ class ToolViewsPositions(ToolBase):
445
445
def __init__ (self , * args , ** kwargs ):
446
446
self .views = WeakKeyDictionary ()
447
447
self .positions = WeakKeyDictionary ()
448
+ self .home_views = WeakKeyDictionary ()
448
449
ToolBase .__init__ (self , * args , ** kwargs )
449
450
450
451
def add_figure (self ):
451
452
"""Add the current figure to the stack of views and positions"""
452
453
if self .figure not in self .views :
453
454
self .views [self .figure ] = cbook .Stack ()
454
455
self .positions [self .figure ] = cbook .Stack ()
456
+ self .home_views [self .figure ] = WeakKeyDictionary ()
455
457
# Define Home
456
458
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 ())
466
461
467
462
def update_view (self ):
468
463
"""
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.
471
468
"""
472
469
473
470
views = self .views [self .figure ]()
@@ -476,28 +473,64 @@ def update_view(self):
476
473
pos = self .positions [self .figure ]()
477
474
if pos is None :
478
475
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' )
484
490
485
491
self .figure .canvas .draw_idle ()
486
492
487
493
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
+ """
489
497
490
- views = []
491
- pos = []
498
+ views = WeakKeyDictionary ()
499
+ pos = WeakKeyDictionary ()
492
500
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 )
498
503
self .views [self .figure ].push (views )
499
504
self .positions [self .figure ].push (pos )
500
505
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
+
501
534
def refresh_locators (self ):
502
535
"""Redraw the canvases, update the locators"""
503
536
for a in self .figure .get_axes ():
0 commit comments