Skip to content

Restore axes sharedness when unpickling. #10659

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
16 changes: 12 additions & 4 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,16 @@ def _grab_next_args(self, *args, **kwargs):
yield from self._plot_args(this, kwargs)


_shared_x_axes = cbook.Grouper()
_shared_y_axes = cbook.Grouper()
_twinned_axes = cbook.Grouper()


class _AxesBase(martist.Artist):
"""
"""
name = "rectilinear"

_shared_x_axes = cbook.Grouper()
_shared_y_axes = cbook.Grouper()
_twinned_axes = cbook.Grouper()

def __str__(self):
return "{0}({1[0]:g},{1[1]:g};{1[2]:g}x{1[3]:g})".format(
type(self).__name__, self._position.bounds)
Expand Down Expand Up @@ -468,6 +469,13 @@ def __init__(self, fig, rect,
""" % {'scale': ' | '.join(
[repr(x) for x in mscale.get_scale_names()])}
martist.Artist.__init__(self)

# Reference the global instances in the instance dict to support
# pickling.
self._shared_x_axes = _shared_x_axes
self._shared_y_axes = _shared_y_axes
self._twinned_axes = _twinned_axes

if isinstance(rect, mtransforms.Bbox):
self._position = rect
else:
Expand Down
19 changes: 19 additions & 0 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,25 @@ class Grouper(object):
def __init__(self, init=()):
self._mapping = {ref(x): [ref(x)] for x in init}

def __getstate__(self):
mapping = {}
for k, vs in self._mapping.items():
k = k()
if k is None:
continue
mapping[k] = l = []
for v in vs:
v = v()
if v is None:
continue
l.append(v)
return {"_mapping": mapping}

def __setstate__(self, state):
self.__dict__ = state
self._mapping = {ref(k): [ref(v) for v in vs]
for k, vs in self._mapping.items()}

def __contains__(self, item):
return ref(item) in self._mapping

Expand Down
7 changes: 7 additions & 0 deletions lib/matplotlib/tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,10 @@ def test_rrulewrapper():
except RecursionError:
print('rrulewrapper pickling test failed')
raise


def test_shared():
fig, axs = plt.subplots(2, sharex=True)
fig = pickle.loads(pickle.dumps(fig))
fig.axes[0].set_xlim(10, 20)
assert fig.axes[1].get_xlim() == (10, 20)