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

Skip to content

Commit fb70c3f

Browse files
committed
figure.py: preserve the order in which Axes were added to the figure
svn path=/trunk/matplotlib/; revision=8693
1 parent f2d3cf7 commit fb70c3f

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

lib/matplotlib/figure.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,35 @@ class AxesStack(Stack):
4141
"""
4242
Specialization of the Stack to handle all
4343
tracking of Axes in a Figure. This requires storing
44-
key, axes pairs. The key is based on the args and kwargs
45-
used in generating the Axes.
44+
key, (ind, axes) pairs. The key is based on the args and kwargs
45+
used in generating the Axes. ind is a serial number for tracking
46+
the order in which axes were added.
4647
"""
48+
def __init__(self):
49+
Stack.__init__(self)
50+
self._ind = 0
51+
4752
def as_list(self):
4853
"""
4954
Return a list of the Axes instances that have been added to the figure
5055
"""
51-
return [a for k, a in self._elements]
56+
ia_list = [a for k, a in self._elements]
57+
ia_list.sort()
58+
return [a for i, a in ia_list]
5259

5360
def get(self, key):
5461
"""
5562
Return the Axes instance that was added with *key*.
5663
If it is not present, return None.
5764
"""
58-
return dict(self._elements).get(key)
65+
item = dict(self._elements).get(key)
66+
if item is None:
67+
return None
68+
return item[1]
5969

6070
def _entry_from_axes(self, e):
61-
k = dict([(a, k) for (k, a) in self._elements])[e]
62-
return k, e
71+
ind, k = dict([(a, (ind, k)) for (k, (ind, a)) in self._elements])[e]
72+
return (k, (ind, e))
6373

6474
def remove(self, a):
6575
Stack.remove(self, self._entry_from_axes(a))
@@ -92,13 +102,14 @@ def add(self, key, a):
92102

93103
if a in self:
94104
return None
95-
return Stack.push(self, (key, a))
105+
self._ind += 1
106+
return Stack.push(self, (key, (self._ind, a)))
96107

97108
def __call__(self):
98109
if not len(self._elements):
99110
return self._default
100111
else:
101-
return self._elements[self._pos][1]
112+
return self._elements[self._pos][1][1]
102113

103114
def __contains__(self, a):
104115
return a in self.as_list()

0 commit comments

Comments
 (0)