@@ -455,29 +455,34 @@ class ToolViewsPositions(ToolBase):
455
455
def __init__ (self , * args , ** kwargs ):
456
456
self .views = WeakKeyDictionary ()
457
457
self .positions = WeakKeyDictionary ()
458
+ self .home_views = WeakKeyDictionary ()
458
459
ToolBase .__init__ (self , * args , ** kwargs )
459
460
460
461
def add_figure (self ):
461
462
"""Add the current figure to the stack of views and positions"""
462
463
if self .figure not in self .views :
463
464
self .views [self .figure ] = cbook .Stack ()
464
465
self .positions [self .figure ] = cbook .Stack ()
466
+ self .home_views [self .figure ] = WeakKeyDictionary ()
465
467
# Define Home
466
468
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 ())
470
471
471
472
def clear (self , figure ):
472
473
"""Reset the axes stack"""
473
474
if figure in self .views :
474
475
self .views [figure ].clear ()
475
476
self .positions [figure ].clear ()
477
+ self .home_views [figure ].clear ()
478
+ self .update_home_views ()
476
479
477
480
def update_view (self ):
478
481
"""
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.
481
486
"""
482
487
483
488
views = self .views [self .figure ]()
@@ -486,28 +491,64 @@ def update_view(self):
486
491
pos = self .positions [self .figure ]()
487
492
if pos is None :
488
493
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' )
494
508
495
509
self .figure .canvas .draw_idle ()
496
510
497
511
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
+ """
499
515
500
- views = []
501
- pos = []
516
+ views = WeakKeyDictionary ()
517
+ pos = WeakKeyDictionary ()
502
518
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 )
508
521
self .views [self .figure ].push (views )
509
522
self .positions [self .figure ].push (pos )
510
523
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
+
511
552
def refresh_locators (self ):
512
553
"""Redraw the canvases, update the locators"""
513
554
for a in self .figure .get_axes ():
0 commit comments