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

Skip to content

Commit f2d3cf7

Browse files
committed
figure.py, axes tracking: ensure a unique key is provided.
svn path=/trunk/matplotlib/; revision=8682
1 parent 5e2a5fa commit f2d3cf7

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

lib/matplotlib/figure.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ def add(self, key, a):
8282
hash(key)
8383
except TypeError:
8484
raise ValueError("first argument, %s, is not a valid key" % key)
85+
86+
a_existing = self.get(key)
87+
if a_existing is not None:
88+
Stack.remove(self, (key, a_existing))
89+
warnings.Warn(
90+
"key %s already existed; Axes is being replaced" % key)
91+
# I don't think the above should ever happen.
92+
8593
if a in self:
8694
return None
8795
return Stack.push(self, (key, a))
@@ -648,15 +656,14 @@ def add_axes(self, *args, **kwargs):
648656
649657
%(Axes)s
650658
"""
659+
if not len(args): return
651660

652661
key = self._make_key(*args, **kwargs)
653-
654662
ax = self._axstack.get(key)
655663
if ax is not None:
656664
self.sca(ax)
657665
return ax
658666

659-
if not len(args): return
660667
if isinstance(args[0], Axes):
661668
a = args[0]
662669
assert(a.get_figure() is self)
@@ -674,8 +681,7 @@ def add_axes(self, *args, **kwargs):
674681

675682
a = projection_factory(projection, self, rect, **kwargs)
676683

677-
if a not in self._axstack:
678-
self._axstack.add(key, a)
684+
self._axstack.add(key, a)
679685
self.sca(a)
680686
return a
681687

@@ -708,15 +714,17 @@ def add_subplot(self, *args, **kwargs):
708714
709715
%(Axes)s
710716
"""
711-
712-
kwargs = kwargs.copy()
713-
714717
if not len(args): return
715718

719+
if len(args) == 1 and isinstance(args[0], int):
720+
args = tuple([int(c) for c in str(args[0])])
721+
716722
if isinstance(args[0], SubplotBase):
717723
a = args[0]
718724
assert(a.get_figure() is self)
725+
key = self._make_key(*args, **kwargs)
719726
else:
727+
kwargs = kwargs.copy()
720728
ispolar = kwargs.pop('polar', False)
721729
projection = kwargs.pop('projection', None)
722730
if ispolar:
@@ -729,6 +737,7 @@ def add_subplot(self, *args, **kwargs):
729737

730738
projection_class = get_projection_class(projection)
731739

740+
# Remake the key without projection kwargs:
732741
key = self._make_key(*args, **kwargs)
733742
ax = self._axstack.get(key)
734743
if ax is not None:
@@ -737,6 +746,11 @@ def add_subplot(self, *args, **kwargs):
737746
return ax
738747
else:
739748
self._axstack.remove(ax)
749+
# Undocumented convenience behavior:
750+
# subplot(111); subplot(111, projection='polar')
751+
# will replace the first with the second.
752+
# Without this, add_subplot would be simpler and
753+
# more similar to add_axes.
740754

741755
a = subplot_class_factory(projection_class)(self, *args, **kwargs)
742756
self._axstack.add(key, a)

0 commit comments

Comments
 (0)