簡體   English   中英

使用自定義 LinearSegmentedColormap 修復 matplotlib.pyplot.set_cmap() 處的 ValueError

[英]Fix ValueError at matplotlib.pyplot.set_cmap() with custom LinearSegmentedColormap

我正在尋找一種將自定義顏色 map 與 matplotlib.pyplot 結合使用的方法。 我寫了這段代碼:

import numpy as np
from matplotlib.colors import LinearSegmentedColormap
import matplotlib as mpl
import matplotlib.pyplot as plt

%matplotlib inline   

# color_map = mpl.cm.get_cmap('Spectral') # get builtin colormap
color_map = LinearSegmentedColormap.from_list('custom', 
                                       [(0, '#ff0000'),
                                        (1,   '#00ff00')], N=10)

x = np.array(range(10))
y = np.sin(x) 

plt.set_cmap(color_map)
plt.plot(x, y)

在這里,我創建了colormap圖並將其傳遞給plt.set_cmap(color_map) 之后我嘗試執行plt.plot(x, y) 在這里我得到這樣的 ValueError:

ValueError: 'custom' is not a valid value for name; supported values are 'Accent', 'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 'BuPu_r', 'CMRmap', 'CMRmap_r', 'Dark2', 'Dark2_r', 'GnBu', 'GnBu_r', 'Greens', 'Greens_r', 'Greys', 'Greys_r', 'OrRd', 'OrRd_r', 'Oranges', 'Oranges_r', 'PRGn', 'PRGn_r', 'Paired', 'Paired_r', 'Pastel1', 'Pastel1_r', 'Pastel2', 'Pastel2_r', 'PiYG', 'PiYG_r', 'PuBu', 'PuBuGn', 'PuBuGn_r', 'PuBu_r', 'PuOr', 'PuOr_r', 'PuRd', 'PuRd_r', 'Purples', 'Purples_r', 'RdBu', 'RdBu_r', 'RdGy', 'RdGy_r', 'RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds', 'Reds_r', 'Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral', 'Spectral_r', 'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r', 'YlOrBr', 'YlOrBr_r', 'YlOrRd', 'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn', 'autumn_r', 'binary', 'binary_r', 'bone', 'bone_r', 'brg', 'brg_r', 'bwr', 'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', 'coolwarm', 'coolwarm_r', 'copper', 'copper_r', 'cubehelix', 'cubehelix_r', 'flag', 'flag_r', 'gist_earth', 'gist_earth_r', 'gist_gray', 'gist_gray_r', 'gist_heat', 'gist_heat_r', 'gist_ncar', 'gist_ncar_r', 'gist_rainbow', 'gist_rainbow_r', 'gist_stern', 'gist_stern_r', 'gist_yarg', 'gist_yarg_r', 'gnuplot', 'gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray', 'gray_r', 'hot', 'hot_r', 'hsv', 'hsv_r', 'inferno', 'inferno_r', 'jet', 'jet_r', 'magma', 'magma_r', 'nipy_spectral', 'nipy_spectral_r', 'ocean', 'ocean_r', 'pink', 'pink_r', 'plasma', 'plasma_r', 'prism', 'prism_r', 'rainbow', 'rainbow_r', 'seismic', 'seismic_r', 'spring', 'spring_r', 'summer', 'summer_r', 'tab10', 'tab10_r', 'tab20', 'tab20_r', 'tab20b', 'tab20b_r', 'tab20c', 'tab20c_r', 'terrain', 'terrain_r', 'turbo', 'turbo_r', 'twilight', 'twilight_r', 'twilight_shifted', 'twilight_shifted_r', 'viridis', 'viridis_r', 'winter', 'winter_r'

使用# color_map = mpl.cm.get_cmap('Spectral') # get builtin colormap沒有錯誤。 任何想法發生了什么?

python 3.9.12

顯然,要使用 set_map 方法,您必須選擇一個 cmap 標識名稱,或者帶有標識名稱的 cmap object,它已經包含在 cmap 名稱列表中。 您將已構建的自定義顏色 map 的名稱包含到列表中,使用:

matplotlib.pyplot.register_cmap(cmap=cmap_object_to_register)

因此,對我有用的正確代碼是:

import numpy as np
from matplotlib.colors import LinearSegmentedColormap
import matplotlib as mpl
import matplotlib.pyplot as plt

%matplotlib inline   

#color_map = mpl.cm.get_cmap('Spectral') # get builtin colormap
color_map2 = LinearSegmentedColormap.from_list('custom',
                                       [(0, '#ff0000'),
                                        (1,   '#00ff00')], N=10)

plt.register_cmap(cmap=color_map2) # register new colormap

x = np.array(range(10))
y = np.sin(x) 

plt.set_cmap(color_map2) # set colormap
plt.plot(x, y)

同樣,您也可以將 set_map 與顏色 map 標識名稱一起使用,就像您的示例一樣,在注冊之后,執行以下操作:

plt.set_cmap('custom')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM