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

Skip to content

Commit 0cac039

Browse files
committed
FIX: Allow deepcopy on norms and scales
1 parent b2921c8 commit 0cac039

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

doc/api/scale_api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
********************
44

55
.. automodule:: matplotlib.scale
6+
:private-members: _CopyableTransformMixin
67
:members:
78
:undoc-members:
89
:show-inheritance:

lib/matplotlib/scale.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ def limit_range_for_scale(self, vmin, vmax, minpos):
7979
return vmin, vmax
8080

8181

82+
class _CopyableTransformMixin():
83+
"""
84+
Mixin to support copy and deep copy on transforms. This alows scales,
85+
and hence norms, to be copyable.
86+
"""
87+
def __deepcopy__(self, memo):
88+
return self.frozen()
89+
90+
__copy__ = __deepcopy__
91+
92+
8293
class LinearScale(ScaleBase):
8394
"""
8495
The default linear scale.
@@ -113,9 +124,9 @@ def get_transform(self):
113124
return IdentityTransform()
114125

115126

116-
class FuncTransform(Transform):
127+
class FuncTransform(_CopyableTransformMixin, Transform):
117128
"""
118-
A simple transform that takes and arbitrary function for the
129+
A transform that takes and arbitrary function for the
119130
forward and inverse transform.
120131
"""
121132

@@ -191,7 +202,7 @@ def set_default_locators_and_formatters(self, axis):
191202
axis.set_minor_locator(NullLocator())
192203

193204

194-
class LogTransform(Transform):
205+
class LogTransform(_CopyableTransformMixin, Transform):
195206
input_dims = output_dims = 1
196207

197208
@_api.rename_parameter("3.3", "nonpos", "nonpositive")
@@ -233,7 +244,7 @@ def inverted(self):
233244
return InvertedLogTransform(self.base)
234245

235246

236-
class InvertedLogTransform(Transform):
247+
class InvertedLogTransform(_CopyableTransformMixin, Transform):
237248
input_dims = output_dims = 1
238249

239250
def __init__(self, base):
@@ -359,7 +370,7 @@ def get_transform(self):
359370
return self._transform
360371

361372

362-
class SymmetricalLogTransform(Transform):
373+
class SymmetricalLogTransform(_CopyableTransformMixin, Transform):
363374
input_dims = output_dims = 1
364375

365376
def __init__(self, base, linthresh, linscale):
@@ -391,7 +402,7 @@ def inverted(self):
391402
self.linscale)
392403

393404

394-
class InvertedSymmetricalLogTransform(Transform):
405+
class InvertedSymmetricalLogTransform(_CopyableTransformMixin, Transform):
395406
input_dims = output_dims = 1
396407

397408
def __init__(self, base, linthresh, linscale):
@@ -494,7 +505,7 @@ def get_transform(self):
494505
return self._transform
495506

496507

497-
class LogitTransform(Transform):
508+
class LogitTransform(_CopyableTransformMixin, Transform):
498509
input_dims = output_dims = 1
499510

500511
@_api.rename_parameter("3.3", "nonpos", "nonpositive")
@@ -520,7 +531,7 @@ def __str__(self):
520531
return "{}({!r})".format(type(self).__name__, self._nonpositive)
521532

522533

523-
class LogisticTransform(Transform):
534+
class LogisticTransform(_CopyableTransformMixin, Transform):
524535
input_dims = output_dims = 1
525536

526537
@_api.rename_parameter("3.3", "nonpos", "nonpositive")

lib/matplotlib/tests/test_colors.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import matplotlib.colorbar as mcolorbar
1717
import matplotlib.cbook as cbook
1818
import matplotlib.pyplot as plt
19+
import matplotlib.scale as mscale
1920
from matplotlib.testing.decorators import image_comparison
2021

2122

@@ -1320,3 +1321,16 @@ def test_2d_to_rgba():
13201321
rgba_1d = mcolors.to_rgba(color.reshape(-1))
13211322
rgba_2d = mcolors.to_rgba(color.reshape((1, -1)))
13221323
assert rgba_1d == rgba_2d
1324+
1325+
1326+
def test_norm_deepcopy():
1327+
norm = mcolors.LogNorm()
1328+
norm.vmin = 0.0002
1329+
norm2 = copy.deepcopy(norm)
1330+
assert norm2.vmin == norm.vmin
1331+
assert isinstance(norm2._scale, mscale.LogScale)
1332+
norm = mcolors.Normalize()
1333+
norm.vmin = 0.0002
1334+
norm2 = copy.deepcopy(norm)
1335+
assert isinstance(norm2._scale, mscale.LinearScale)
1336+
assert norm2.vmin == norm.vmin

lib/matplotlib/tests/test_scale.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import numpy as np
99
from numpy.testing import assert_allclose
10+
import copy
1011
import io
1112
import pytest
1213

@@ -210,3 +211,9 @@ def test_pass_scale():
210211
ax.set_yscale(scale)
211212
assert ax.xaxis.get_scale() == 'log'
212213
assert ax.yaxis.get_scale() == 'log'
214+
215+
216+
def test_scale_deepcopy():
217+
sc = mscale.LogScale(axis='x', base=10)
218+
sc2 = copy.deepcopy(sc)
219+
assert str(sc.get_transform()) == str(sc2.get_transform())

lib/matplotlib/transforms.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,6 +2062,12 @@ def frozen(self):
20622062
# docstring inherited
20632063
return self
20642064

2065+
def __deepcopy__(self, memo):
2066+
# The identity transform does not need to lock out deepcopy
2067+
return self.frozen()
2068+
2069+
__copy__ = __deepcopy__
2070+
20652071
__str__ = _make_str_method()
20662072

20672073
def get_matrix(self):

0 commit comments

Comments
 (0)