@@ -445,29 +445,34 @@ 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 )
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 ())
460
461
461
462
def clear (self , figure ):
462
463
"""Reset the axes stack"""
463
464
if figure in self .views :
464
465
self .views [figure ].clear ()
465
466
self .positions [figure ].clear ()
467
+ self .home_views [figure ].clear ()
468
+ self .update_home_views ()
466
469
467
470
def update_view (self ):
468
471
"""
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.
471
476
"""
472
477
473
478
views = self .views [self .figure ]()
@@ -476,28 +481,64 @@ def update_view(self):
476
481
pos = self .positions [self .figure ]()
477
482
if pos is None :
478
483
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' )
484
498
485
499
self .figure .canvas .draw_idle ()
486
500
487
501
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
+ """
489
505
490
- views = []
491
- pos = []
506
+ views = WeakKeyDictionary ()
507
+ pos = WeakKeyDictionary ()
492
508
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 )
498
511
self .views [self .figure ].push (views )
499
512
self .positions [self .figure ].push (pos )
500
513
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
+
501
542
def refresh_locators (self ):
502
543
"""Redraw the canvases, update the locators"""
503
544
for a in self .figure .get_axes ():
0 commit comments