@@ -445,29 +445,24 @@ 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 ()
466
459
467
460
def update_view (self ):
468
461
"""
469
- Update the viewlim and position from the view and
470
- position stack for each axes
462
+ Update the view limits and position for each axes from the current
463
+ stack position. If any axes are present in the figure that aren't in
464
+ the current stack position, use the home view limits for those axes and
465
+ don't update *any* positions.
471
466
"""
472
467
473
468
views = self .views [self .figure ]()
@@ -476,27 +471,64 @@ def update_view(self):
476
471
pos = self .positions [self .figure ]()
477
472
if pos is None :
478
473
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' )
474
+ home_views = self .home_views [self .figure ]
475
+ all_axes = self .figure .get_axes ()
476
+ for a in all_axes :
477
+ if a in views :
478
+ cur_view = views [a ]
479
+ else :
480
+ cur_view = home_views [a ]
481
+ a ._set_view (cur_view )
482
+
483
+ if set (all_axes ).issubset (pos .keys ()):
484
+ for a in all_axes :
485
+ # Restore both the original and modified positions
486
+ a .set_position (pos [a ][0 ], 'original' )
487
+ a .set_position (pos [a ][1 ], 'active' )
484
488
485
489
self .figure .canvas .draw_idle ()
486
490
487
491
def push_current (self ):
488
- """push the current view limits and position onto the stack"""
492
+ """
493
+ Push the current view limits and position onto their respective stacks
494
+ """
489
495
490
- views = []
491
- pos = []
496
+ views = WeakKeyDictionary ()
497
+ pos = WeakKeyDictionary ()
492
498
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 ()))
499
+ views [a ] = a ._get_view ()
500
+ pos [a ] = self ._axes_pos (a )
498
501
self .views [self .figure ].push (views )
499
502
self .positions [self .figure ].push (pos )
503
+ self .update_home_views ()
504
+
505
+ def _axes_pos (self , ax ):
506
+ """
507
+ Return the original and modified positions for the specified axes
508
+
509
+ Parameters
510
+ ----------
511
+ ax : (matplotlib.axes.AxesSubplot)
512
+ The axes to get the positions for
513
+
514
+ Returns
515
+ -------
516
+ limits : (tuple)
517
+ A tuple of the original and modified positions
518
+ """
519
+
520
+ return (ax .get_position (True ).frozen (),
521
+ ax .get_position ().frozen ())
522
+
523
+ def update_home_views (self ):
524
+ """
525
+ Make sure that self.home_views has an entry for all axes present in the
526
+ figure
527
+ """
528
+
529
+ for a in self .figure .get_axes ():
530
+ if a not in self .home_views [self .figure ]:
531
+ self .home_views [self .figure ][a ] = a ._get_view ()
500
532
501
533
def refresh_locators (self ):
502
534
"""Redraw the canvases, update the locators"""
@@ -542,6 +574,7 @@ class ViewsPositionsBase(ToolBase):
542
574
543
575
def trigger (self , sender , event , data = None ):
544
576
self .toolmanager .get_tool (_views_positions ).add_figure ()
577
+ self .toolmanager .get_tool (_views_positions ).update_home_views ()
545
578
getattr (self .toolmanager .get_tool (_views_positions ),
546
579
self ._on_trigger )()
547
580
self .toolmanager .get_tool (_views_positions ).update_view ()
0 commit comments