10000 Merge pull request #19668 from meeseeksmachine/auto-backport-of-pr-19… · matplotlib/matplotlib@2cc43e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2cc43e1

Browse files
authored
Merge pull request #19668 from meeseeksmachine/auto-backport-of-pr-19663-on-v3.4.x
Backport PR #19663 on branch v3.4.x (ENH: add a copy method to colormaps)
2 parents 59b32af + f906502 commit 2cc43e1

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/matplotlib/colors.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ def _warn_if_global_cmap_modified(cmap):
543543
"colormap. In future versions, you will not be able to "
544544
"modify a registered colormap in-place. To remove this "
545545
"warning, you can make a copy of the colormap first. "
546-
f'cmap = copy.copy(mpl.cm.get_cmap("{cmap.name}"))'
546+
f'cmap = mpl.cm.get_cmap("{cmap.name}").copy()'
547547
)
548548

549549

@@ -827,6 +827,10 @@ def color_block(color):
827827
f'over {color_block(self.get_over())}'
828828
'</div>')
829829

830+
def copy(self):
831+
"""Return a copy of the colormap."""
832+
return self.__copy__()
833+
830834

831835
class LinearSegmentedColormap(Colormap):
832836
"""

lib/matplotlib/tests/test_colors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ def test_colormap_copy():
150150
with np.errstate(invalid='ignore'):
151151
ret2 = copied_cmap([-1, 0, .5, 1, np.nan, np.inf])
152152
assert_array_equal(ret1, ret2)
153+
# again with the .copy method:
154+
cmap = plt.cm.Reds
155+
copied_cmap = cmap.copy()
156+
with np.errstate(invalid='ignore'):
157+
ret1 = copied_cmap([-1, 0, .5, 1, np.nan, np.inf])
158+
cmap2 = copy.copy(copied_cmap)
159+
cmap2.set_bad('g')
160+
with np.errstate(invalid='ignore'):
161+
ret2 = copied_cmap([-1, 0, .5, 1, np.nan, np.inf])
162+
assert_array_equal(ret1, ret2)
153163

154164

155165
def test_colormap_endian():

0 commit comments

Comments
 (0)
0