Skip to content

Commit 0489b7a

Browse files
authored
Merge pull request #25332 from anntzer/alg
Support pickling of figures with aligned x/y labels.
2 parents 5e8c140 + ea2e9ea commit 0489b7a

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/matplotlib/cbook.py

+13
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,19 @@ class Grouper:
788788
def __init__(self, init=()):
789789
self._mapping = {weakref.ref(x): [weakref.ref(x)] for x in init}
790790

791+
def __getstate__(self):
792+
return {
793+
**vars(self),
794+
# Convert weak refs to strong ones.
795+
"_mapping": {k(): [v() for v in vs] for k, vs in self._mapping.items()},
796+
}
797+
798+
def __setstate__(self, state):
799+
vars(self).update(state)
800+
# Convert strong refs to weak ones.
801+
self._mapping = {weakref.ref(k): [*map(weakref.ref, vs)]
802+
for k, vs in self._mapping.items()}
803+
791804
def __contains__(self, item):
792805
return weakref.ref(item) in self._mapping
793806

lib/matplotlib/tests/test_pickle.py

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def _generate_complete_test_figure(fig_ref):
5959
# Ensure lists also pickle correctly.
6060
plt.subplot(3, 3, 1)
6161
plt.plot(list(range(10)))
62+
plt.ylabel("hello")
6263

6364
plt.subplot(3, 3, 2)
6465
plt.contourf(data, hatches=['//', 'ooo'])
@@ -69,6 +70,7 @@ def _generate_complete_test_figure(fig_ref):
6970

7071
plt.subplot(3, 3, 4)
7172
plt.imshow(data)
73+
plt.ylabel("hello\nworld!")
7274

7375
plt.subplot(3, 3, 5)
7476
plt.pcolor(data)
@@ -91,6 +93,8 @@ def _generate_complete_test_figure(fig_ref):
9193
plt.errorbar(x, x * -0.5, xerr=0.2, yerr=0.4)
9294
plt.legend(draggable=True)
9395

96+
fig_ref.align_ylabels() # Test handling of _align_label_groups Groupers.
97+
9498

9599
@mpl.style.context("default")
96100
@check_figures_equal(extensions=["png"])

0 commit comments

Comments
 (0)