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

Skip to content

Non string projection definitions in matplotlib #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/matplotlib/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not quite obvious to me what is the benefit of this. Can you show some cases where this is useful?
I think Subplot must be a thin wrapper around Axes, and it should not change anything other that those related with position of axes. In other words, I think subplot_class_factory must be sufficient to create every subplot classes.

# _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()
Expand Down Expand Up @@ -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
Expand Down
41 changes: 27 additions & 14 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down