Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbd262e9

Browse files
committed
Restore axes sharedness when unpickling.
Previously, pickling and unpickling shared axes would result inaxes sharing a ticker instance (because that's how shared axesare set up), but without changes of one's xlims propagated to theother. The reason is that that sharedness information is stored inAxesBase._shared_x_axes, which does *not* get pickled together with theAxes instance: the latter only has a textual reference "I am an instanceof AxesBase", so the Grouper information is lost.To keep the Grouper information valid, instead move the Groupers to theinstance dictionaries (as references to global groupers). Also makeGroupers picklable following a similar strategy as Transforms, i.e. bytransforming weakrefs into real refs when pickling and transforming themback into weakref when unpickling.
1 parente22a16a commitbd262e9

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

‎lib/matplotlib/axes/_base.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,15 +395,16 @@ def _grab_next_args(self, *args, **kwargs):
395395
yieldfromself._plot_args(this,kwargs)
396396

397397

398+
_shared_x_axes=cbook.Grouper()
399+
_shared_y_axes=cbook.Grouper()
400+
_twinned_axes=cbook.Grouper()
401+
402+
398403
class_AxesBase(martist.Artist):
399404
"""
400405
"""
401406
name="rectilinear"
402407

403-
_shared_x_axes=cbook.Grouper()
404-
_shared_y_axes=cbook.Grouper()
405-
_twinned_axes=cbook.Grouper()
406-
407408
def__str__(self):
408409
return"{0}({1[0]:g},{1[1]:g};{1[2]:g}x{1[3]:g})".format(
409410
type(self).__name__,self._position.bounds)
@@ -468,6 +469,13 @@ def __init__(self, fig, rect,
468469
"""% {'scale':' | '.join(
469470
[repr(x)forxinmscale.get_scale_names()])}
470471
martist.Artist.__init__(self)
472+
473+
# Reference the global instances in the instance dict to support
474+
# pickling.
475+
self._shared_x_axes=_shared_x_axes
476+
self._shared_y_axes=_shared_y_axes
477+
self._twinned_axes=_twinned_axes
478+
471479
ifisinstance(rect,mtransforms.Bbox):
472480
self._position=rect
473481
else:

‎lib/matplotlib/cbook/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,25 @@ class Grouper(object):
981981
def__init__(self,init=()):
982982
self._mapping= {ref(x): [ref(x)]forxininit}
983983

984+
def__getstate__(self):
985+
mapping= {}
986+
fork,vsinself._mapping.items():
987+
k=k()
988+
ifkisNone:
989+
continue
990+
mapping[k]=l= []
991+
forvinvs:
992+
v=v()
993+
ifvisNone:
994+
continue
995+
l.append(v)
996+
return {"_mapping":mapping}
997+
998+
def__setstate__(self,state):
999+
self.__dict__=state
1000+
self._mapping= {ref(k): [ref(v)forvinvs]
1001+
fork,vsinself._mapping.items()}
1002+
9841003
def__contains__(self,item):
9851004
returnref(item)inself._mapping
9861005

‎lib/matplotlib/tests/test_pickle.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,10 @@ def test_rrulewrapper():
187187
exceptRecursionError:
188188
print('rrulewrapper pickling test failed')
189189
raise
190+
191+
192+
deftest_shared():
193+
fig,axs=plt.subplots(2,sharex=True)
194+
fig=pickle.loads(pickle.dumps(fig))
195+
fig.axes[0].set_xlim(10,20)
196+
assertfig.axes[1].get_xlim()== (10,20)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp