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

Skip to content

Commit cfc1609

Browse files
committed
FIX: Remove the deepcopy override from transforms
Let Python take care of the deepcopy for us.
1 parent 25a54a4 commit cfc1609

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

lib/matplotlib/tests/test_figure.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
from datetime import datetime
23
import io
34
from pathlib import Path
@@ -1261,3 +1262,28 @@ def test_kwargs_pass():
12611262

12621263
assert fig.get_label() == 'whole Figure'
12631264
assert sub_fig.get_label() == 'sub figure'
1265+
1266+
1267+
def test_deepcopy():
1268+
fig1, ax = plt.subplots()
1269+
ax.plot([0, 1], [2, 3])
1270+
ax.set_yscale('log')
1271+
1272+
fig2 = copy.deepcopy(fig1)
1273+
1274+
# Make sure it is a new object
1275+
assert fig2.axes[0] is not ax
1276+
# And that the axis scale got propagated
1277+
assert fig2.axes[0].get_yscale() == 'log'
1278+
# Update the deepcopy and check the original isn't modified
1279+
fig2.axes[0].set_yscale('linear')
1280+
assert ax.get_yscale() == 'log'
1281+
1282+
# And test the limits of the axes don't get propagated
1283+
ax.set_xlim(1e-1, 1e2)
1284+
# Draw these to make sure limits are updated
1285+
fig1.draw_without_rendering()
1286+
fig2.draw_without_rendering()
1287+
1288+
assert ax.get_xlim() == (1e-1, 1e2)
1289+
assert fig2.axes[0].get_xlim() == (0, 1)

lib/matplotlib/transforms.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,22 +151,6 @@ def __copy__(self):
151151
other.set_children(val) # val == getattr(other, key)
152152
return other
153153

154-
def __deepcopy__(self, memo):
155-
# We could deepcopy the entire transform tree, but nothing except
156-
# `self` is accessible publicly, so we may as well just freeze `self`.
157-
other = self.frozen()
158-
if other is not self:
159-
return other
160-
# Some classes implement frozen() as returning self, which is not
161-
# acceptable for deepcopying, so we need to handle them separately.
162-
other = copy.deepcopy(super(), memo)
163-
# If `c = a + b; a1 = copy(a)`, then modifications to `a1` do not
164-
# propagate back to `c`, i.e. we need to clear the parents of `a1`.
165-
other._parents = {}
166-
# If `c = a + b; c1 = copy(c)`, this creates a separate tree
167-
# (`c1 = a1 + b1`) so nothing needs to be done.
168-
return other
169-
170154
def invalidate(self):
171155
"""
172156
Invalidate this `TransformNode` and triggers an invalidation of its

0 commit comments

Comments
 (0)