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

Skip to content

Commit 989659a

Browse files
authored
Merge pull request #21597 from greglucas/figure-deepcopy
2 parents afbc99d + cfc1609 commit 989659a

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
@@ -1282,3 +1283,28 @@ def test_kwargs_pass():
12821283

12831284
assert fig.get_label() == 'whole Figure'
12841285
assert sub_fig.get_label() == 'sub figure'
1286+
1287+
1288+
def test_deepcopy():
1289+
fig1, ax = plt.subplots()
1290+
ax.plot([0, 1], [2, 3])
1291+
ax.set_yscale('log')
1292+
1293+
fig2 = copy.deepcopy(fig1)
1294+
1295+
# Make sure it is a new object
1296+
assert fig2.axes[0] is not ax
1297+
# And that the axis scale got propagated
1298+
assert fig2.axes[0].get_yscale() == 'log'
1299+
# Update the deepcopy and check the original isn't modified
1300+
fig2.axes[0].set_yscale('linear')
1301+
assert ax.get_yscale() == 'log'
1302+
1303+
# And test the limits of the axes don't get propagated
1304+
ax.set_xlim(1e-1, 1e2)
1305+
# Draw these to make sure limits are updated
1306+
fig1.draw_without_rendering()
1307+
fig2.draw_without_rendering()
1308+
1309+
assert ax.get_xlim() == (1e-1, 1e2)
1310+
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)