FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

ENH: add a copy method to colormaps by jklymak · Pull Request #19663 · matplotlib/matplotlib · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 5 additions & 1 deletion lib/matplotlib/colors.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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,10 @@ def color_block(color):
f'over {color_block(self.get_over())}'
'</div>')

def copy(self):
"""Return a copy of the colormap."""
return self.__copy__()


class LinearSegmentedColormap(Colormap):
"""
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_colors.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL