@@ -52,6 +52,71 @@ def _stale_figure_callback(self, val):
52
52
self .figure .stale = val
53
53
54
54
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
+
55
120
class SubplotParams :
56
121
"""
57
122
A class to hold the parameters for a subplot.
@@ -141,7 +206,7 @@ def __init__(self):
141
206
self .figure = self
142
207
# list of child gridspecs for this figure
143
208
self ._gridspecs = []
144
- self ._localaxes = cbook . Stack () # keep track of axes at this level
209
+ self ._localaxes = _AxesStack () # track all axes and current axes
145
210
self .artists = []
146
211
self .lines = []
147
212
self .patches = []
@@ -716,8 +781,8 @@ def add_subplot(self, *args, **kwargs):
716
781
717
782
def _add_axes_internal (self , ax , key ):
718
783
"""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 )
721
786
self .sca (ax )
722
787
ax ._remove_method = self .delaxes
723
788
# this is to support plt.subplot's re-selection logic
@@ -2161,7 +2226,7 @@ def __init__(self,
2161
2226
2162
2227
self .set_tight_layout (tight_layout )
2163
2228
2164
- self ._axstack = cbook . Stack () # track all figure axes and current axes
2229
+ self ._axstack = _AxesStack () # track all figure axes and current axes
2165
2230
self .clf ()
2166
2231
self ._cachedRenderer = None
2167
2232
0 commit comments