Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ac394fc

Browse files
committed
Restore _AxesStack to track a Figure's Axes order.
1 parent bc97294 commit ac394fc

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,9 +622,6 @@ def __len__(self):
622622
def __getitem__(self, ind):
623623
return self._elements[ind]
624624

625-
def as_list(self):
626-
return list(self._elements)
627-
628625
def forward(self):
629626
"""Move the position forward and return the current element."""
630627
self._pos = min(self._pos + 1, len(self._elements) - 1)

lib/matplotlib/figure.py

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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, and return the stack.
91+
92+
If *a* is already on the stack, don't add it again, but
93+
return *None*.
94+
"""
95+
# All the error checking may be unnecessary; but this method
96+
# is called so seldom that the overhead is negligible.
97+
_api.check_isinstance(Axes, a=a)
98+
99+
if a in self:
100+
return None
101+
self._ind += 1
102+
return 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 self._default
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+
55120
class 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
@@ -2155,7 +2220,7 @@ def __init__(self,
21552220

21562221
self.set_tight_layout(tight_layout)
21572222

2158-
self._axstack = cbook.Stack() # track all figure axes and current axes
2223+
self._axstack = _AxesStack() # track all figure axes and current axes
21592224
self.clf()
21602225
self._cachedRenderer = None
21612226

lib/matplotlib/tests/test_figure.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ def test_gca():
195195
assert fig.gca(projection='rectilinear') is ax1
196196
assert fig.gca() is ax1
197197

198+
# sca() should not change stored order of Axes, which is order added.
199+
assert fig.axes == [ax0, ax1, ax2, ax3]
200+
198201

199202
def test_add_subplot_subclass():
200203
fig = plt.figure()

0 commit comments

Comments
 (0)