From 9c7b1b27d0245a752d010bd03ae66dc6c000d8e4 Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Mon, 27 Jun 2011 22:55:07 +0100 Subject: [PATCH] Initial checkin of non-string based projections --- lib/matplotlib/axes.py | 8 +++++--- lib/matplotlib/figure.py | 41 ++++++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/lib/matplotlib/axes.py b/lib/matplotlib/axes.py index b13e55bca9a6..ce074d9dbcb6 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -8378,11 +8378,13 @@ def __init__(self, fig, *args, **kwargs): self.update_params() + # initialise the axes_class + self._init_axes(fig, **kwargs) + + def _init_axes(self, fig, **kwargs): # _axes_class is set in the subplot_class_factory self._axes_class.__init__(self, fig, self.figbox, **kwargs) - - def get_geometry(self): 'get the subplot geometry, eg 2,2,3' rows, cols, num1, num2 = self.get_subplotspec().get_geometry() @@ -8443,7 +8445,7 @@ def label_outer(self): _subplot_classes = {} def subplot_class_factory(axes_class=None): - # This makes a new class that inherits from SubclassBase and the + # This makes a new class that inherits from SubplotBase and the # given axes_class (which is assumed to be a subclass of Axes). # This is perhaps a little bit roundabout to make a new class on # the fly like this, but it means that a new Subplot class does diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index de9c1a934cc6..7dfbd3e94909 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -757,24 +757,33 @@ def add_subplot(self, *args, **kwargs): projection) projection = 'polar' - projection_class = get_projection_class(projection) - # Remake the key without projection kwargs: key = self._make_key(*args, **kwargs) ax = self._axstack.get(key) if ax is not None: - if isinstance(ax, projection_class): + if projection is None or isinstance(projection, basestring): + projection_class = get_projection_class(projection) + if isinstance(ax, projection_class): + self.sca(ax) + return ax + + elif hasattr(projection, 'axes_isinstance') and projection.axes_isinstance(ax): self.sca(ax) return ax - else: - self._axstack.remove(ax) - # Undocumented convenience behavior: - # subplot(111); subplot(111, projection='polar') - # will replace the first with the second. - # Without this, add_subplot would be simpler and - # more similar to add_axes. - - a = subplot_class_factory(projection_class)(self, *args, **kwargs) + + self._axstack.remove(ax) + # Undocumented convenience behavior: + # subplot(111); subplot(111, projection='polar') + # will replace the first with the second. + # Without this, add_subplot would be simpler and + # more similar to add_axes. + + if projection is None or isinstance(projection, basestring): + projection_class = get_projection_class(projection) + a = subplot_class_factory(projection_class)(self, *args, **kwargs) + else: + a = projection.subplot(self, *args, **kwargs) + self._axstack.add(key, a) self.sca(a) return a @@ -1047,9 +1056,13 @@ def gca(self, **kwargs): projection) projection = 'polar' - projection_class = get_projection_class(projection) - if isinstance(ax, projection_class): + if projection is None or isinstance(projection, basestring): + projection_class = get_projection_class(projection) + if isinstance(ax, projection_class): + return ax + elif hasattr(projection, 'axes_isinstance') and projection.axes_isinstance(ax): return ax + return self.add_subplot(111, **kwargs) def sca(self, a):