Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9512d32

Browse files
committed
ENH: add a copy method to colormaps
1 parentbd7df78 commit9512d32

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

‎lib/matplotlib/colors.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ def _warn_if_global_cmap_modified(cmap):
561561
"colormap. In future versions, you will not be able to "
562562
"modify a registered colormap in-place. To remove this "
563563
"warning, you can make a copy of the colormap first. "
564-
f'cmap =copy.copy(mpl.cm.get_cmap("{cmap.name}"))'
564+
f'cmap = mpl.cm.get_cmap("{cmap.name}").copy()'
565565
)
566566

567567

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

848+
defcopy(self):
849+
"""
850+
Return a copy of the colormap.
851+
"""
852+
returnself.__copy__()
853+
848854

849855
classLinearSegmentedColormap(Colormap):
850856
"""

‎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
withnp.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+
withnp.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+
withnp.errstate(invalid='ignore'):
161+
ret2=copied_cmap([-1,0,.5,1,np.nan,np.inf])
162+
assert_array_equal(ret1,ret2)
153163

154164

155165
deftest_colormap_endian():

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp