Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ def _warn_if_global_cmap_modified(cmap):
"colormap. In future versions, you will not be able to "
"modify a registered colormap in-place. To remove this "
"warning, you can make a copy of the colormap first. "
f'cmap = copy.copy(mpl.cm.get_cmap("{cmap.name}"))'
f'cmap = mpl.cm.get_cmap("{cmap.name}").copy()'
)


Expand Down Expand Up @@ -845,6 +845,12 @@ def color_block(color):
f'over {color_block(self.get_over())}'
'</div>')

def copy(self):
"""
Return a copy of the colormap.
"""
Comment thread
jklymak marked this conversation as resolved.
Resolved
return self.__copy__()


class LinearSegmentedColormap(Colormap):
"""
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ def test_colormap_copy():
with np.errstate(invalid='ignore'):
ret2 = copied_cmap([-1, 0, .5, 1, np.nan, np.inf])
assert_array_equal(ret1, ret2)
# again with the .copy method:
cmap = plt.cm.Reds
copied_cmap = cmap.copy()
with np.errstate(invalid='ignore'):
ret1 = copied_cmap([-1, 0, .5, 1, np.nan, np.inf])
cmap2 = copy.copy(copied_cmap)
cmap2.set_bad('g')
with np.errstate(invalid='ignore'):
ret2 = copied_cmap([-1, 0, .5, 1, np.nan, np.inf])
assert_array_equal(ret1, ret2)


def test_colormap_endian():
Expand Down