@@ -52,6 +52,71 @@ def _stale_figure_callback(self, val):
5252 self .figure .stale = val
5353
5454
55+ class _AxesStack (cbook .Stack ):
56+ """
57+ Specialization of Stack, to handle all tracking of Axes in a Figure.
58+
59+ This stack stores ``ind, axes`` pairs, where ``ind`` is a serial index
60+ tracking the order in which axes were added.
61+
62+ AxesStack is a callable; calling it returns the current axes.
63+ """
64+
65+ def __init__ (self ):
66+ super ().__init__ ()
67+ self ._ind = 0
68+
69+ def as_list (self ):
70+ """
71+ Return a list of the Axes instances that have been added to the figure.
72+ """
73+ return [a for i , a in sorted (self ._elements )]
74+
75+ def _entry_from_axes (self , e ):
76+ return next (((ind , a ) for ind , a in self ._elements if a == e ), None )
77+
78+ def remove (self , a ):
79+ """Remove the axes from the stack."""
80+ super ().remove (self ._entry_from_axes (a ))
81+
82+ def bubble (self , a ):
83+ """
84+ Move the given axes, which must already exist in the stack, to the top.
85+ """
86+ return super ().bubble (self ._entry_from_axes (a ))
87+
88+ def add (self , a ):
89+ """
90+ Add Axes *a* to the stack.
91+
92+ If *a* is already on the stack, don't add it again.
93+ """
94+ # All the error checking may be unnecessary; but this method
95+ # is called so seldom that the overhead is negligible.
96+ _api .check_isinstance (Axes , a = a )
97+
98+ if a in self :
99+ return
100+
101+ self ._ind += 1
102+ super ().push ((self ._ind , a ))
103+
104+ def __call__ (self ):
105+ """
106+ Return the active axes.
107+
108+ If no axes exists on the stack, then returns None.
109+ """
110+ if not len (self ._elements ):
111+ return None
112+ else :
113+ index , axes = self ._elements [self ._pos ]
114+ return axes
115+
116+ def __contains__ (self , a ):
117+ return a in self .as_list ()
118+
119+
55120class SubplotParams :
56121 """
57122 A class to hold the parameters for a subplot.
@@ -141,7 +206,7 @@ def __init__(self):
141206 self .figure = self
142207 # list of child gridspecs for this figure
143208 self ._gridspecs = []
144- self ._localaxes = cbook . Stack () # keep track of axes at this level
209+ self ._localaxes = _AxesStack () # track all axes and current axes
145210 self .artists = []
146211 self .lines = []
147212 self .patches = []
@@ -716,8 +781,8 @@ def add_subplot(self, *args, **kwargs):
716781
717782 def _add_axes_internal (self , ax , key ):
718783 """Private helper for `add_axes` and `add_subplot`."""
719- self ._axstack .push (ax )
720- self ._localaxes .push (ax )
784+ self ._axstack .add (ax )
785+ self ._localaxes .add (ax )
721786 self .sca (ax )
722787 ax ._remove_method = self .delaxes
723788 # this is to support plt.subplot's re-selection logic
@@ -2150,7 +2215,7 @@ def __init__(self,
21502215
21512216 self .set_tight_layout (tight_layout )
21522217
2153- self ._axstack = cbook . Stack () # track all figure axes and current axes
2218+ self ._axstack = _AxesStack () # track all figure axes and current axes
21542219 self .clf ()
21552220 self ._cachedRenderer = None
21562221
0 commit comments