Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Simplify unshare code.
get shared return a shallow copy.
  • Loading branch information
lkjell committed Jun 24, 2018
commit fec300740731d981cf4e400726bdfd1b99402bfa
80 changes: 24 additions & 56 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4235,31 +4235,33 @@ def twiny(self):

def get_shared_x_axes(self):
"""Return a copy of the shared axes Weakset object for x axes"""
return self._shared_x_axes
return WeakSet(self._shared_x_axes)

def get_shared_y_axes(self):
"""Return a copy of the shared axes Weakset object for y axes"""
return self._shared_y_axes
return WeakSet(self._shared_y_axes)

def is_sharing_x_axes(self):
return len(self.get_shared_x_axes()) > 1

def is_sharing_y_axes(self):
return len(self.get_shared_y_axes()) > 1

def _unshare_axes(self, shared_axes, parent):
children = []
def _unshare_axes(self, shared_axes):
for ax in getattr(self, "_shared_{}_axes".format(shared_axes)):
parent = getattr(ax, "_share{}".format(shared_axes))

for ax in getattr(self, shared_axes):
if getattr(ax, parent) is self:
setattr(ax, parent, None)
children.append(ax)
if parent is self:
setattr(ax, "_share{}".format(shared_axes), None)
self._copy_axis_major_minor(
getattr(ax, "{}axis".format(shared_axes)))

getattr(self, shared_axes).remove(self)
setattr(self, shared_axes, WeakSet([self]))
setattr(self, parent, None)
getattr(self, "_shared_{}_axes".format(shared_axes)).remove(self)
setattr(self, "_shared_{}_axes".format(shared_axes), WeakSet([self]))
setattr(self, "_share{}".format(shared_axes), None)

return children
self._copy_axis_major_minor(
getattr(self, "{}axis".format(shared_axes)))

@staticmethod
def _copy_axis_major_minor(axis):
Expand All @@ -4272,59 +4274,25 @@ def _copy_axis_major_minor(axis):
axis.major.set_axis(axis)
axis.minor.set_axis(axis)

def unshare_x_axes(self, axes=None):
"""
Unshare x axis.

Parameters
----------
axes: Axes
Axes to unshare, if related. None will unshare itself.
"""
if axes is None or axes is self:
children = self._unshare_axes('_shared_x_axes', '_sharex')
for ax in children:
self._copy_axis_major_minor(ax.xaxis)
self._copy_axis_major_minor(self.xaxis)
elif axes in self._shared_x_axes:
axes.unshare_x_axes()

def unshare_y_axes(self, axes=None):
"""
Unshare y axis.

Parameters
----------
axes: Axes
Axes to unshare, if related. None will unshare itself.
"""
if axes is None or axes is self:
children = self._unshare_axes('_shared_y_axes', '_sharey')
for ax in children:
self._copy_axis_major_minor(ax.yaxis)
self._copy_axis_major_minor(self.yaxis)
elif axes in self._shared_y_axes:
axes.unshare_y_axes()
def unshare_x_axes(self):
""" Unshare x axis. """
self._unshare_axes("x")

def unshare_axes(self, axes=None):
"""
Unshare both x and y axes.
def unshare_y_axes(self):
""" Unshare y axis. """
self._unshare_axes("y")

Parameters
----------
axes: Axes
Axes to unshare, if related. None will unshare itself.
"""
self.unshare_x_axes(axes)
self.unshare_y_axes(axes)
def unshare_axes(self):
""" Unshare both x and y axes. """
self.unshare_x_axes()
self.unshare_y_axes()

def _share_axes(self, axes, shared_axes):
shared = getattr(self, shared_axes)
shared |= getattr(axes, shared_axes)

for ax in shared:
setattr(ax, shared_axes, shared)
# ax._adjustable = 'datalim'

def share_x_axes(self, axes):
"""
Expand Down
38 changes: 9 additions & 29 deletions lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2900,35 +2900,15 @@ def permutation_matrices(n):

return polygons

def unshare_z_axes(self, axes=None):
"""
Unshare z axis.

Parameters
----------
axes: Axes
Axes to unshare, if related. None will unshare itself.
"""
if axes is None or axes is self:
children = self._unshare_axes('_shared_z_axes', '_sharez')
for ax in children:
self._copy_axis_major_minor(ax.zaxis)
self._copy_axis_major_minor(self.zaxis)
elif axes in self._shared_z_axes:
axes.unshare_z_axes()

def unshare_axes(self, axes=None):
"""
Unshare x, y and z axes.

Parameters
----------
axes: Axes
Axes to unshare, if related. None will unshare itself.
"""
self.unshare_x_axes(axes)
self.unshare_y_axes(axes)
self.unshare_z_axes(axes)
def unshare_z_axes(self):
""" Unshare z axis. """
self._unshare_axes("z")

def unshare_axes(self):
""" Unshare x, y and z axes. """
self.unshare_x_axes()
self.unshare_y_axes()
self.unshare_z_axes()

def share_z_axes(self, axes):
"""
Expand Down