尝试创建自定义颜色映射时出现类型错误

时间:2021-02-09 22:47:51

标签: python matplotlib

我正在尝试创建双色映射,但不断收到此错误:

类型错误:to_rgb() 缺少 1 个必需的位置参数:'c'

我试过搜索这个但遇到了麻烦所以我来到了这里。任何帮助将不胜感激!

import numpy as np 
import pandas as pd 
import pickle    
import matplotlib
import matplotlib.pyplot as plt
color_map = plt.cm.winter
from matplotlib.patches import RegularPolygon
import math
from PIL import Image
# Needed for custom colour mapping
from matplotlib.colors import ListedColormap,LinearSegmentedColormap
import matplotlib.colors as mcolors

c = mcolors.ColorConverter().to_rgb()
positive_cm = ListedColormap([c('#e1e5e5'),c('#d63b36')])
negative_cm = ListedColormap([c('#e1e5e5'),c('#28aee4')])

1 个答案:

答案 0 :(得分:0)

根据 matplotlib 的文档:

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.colors.to_rgb.html

您需要将颜色作为参数传递给 to_rgb 函数。如果您的意图是将该函数分配给 c,那么您需要做的就是删除括号,这样您就不会尝试实际调用它(不带参数):

c = mcolors.ColorConverter().to_rgb

我可能建议让它保留一个更有意义的名称(并且可能会清理其中的一些导入):

from matplotlib import colors

ListedColorMap = colors.ListedColorMap
to_rgb = colors.ColorConverter().to_rgb

positive_cm = ListedColormap([to_rgb('#e1e5e5'), to_rgb('#d63b36')])
negative_cm = ListedColormap([to_rgb('#e1e5e5'), to_rgb('#28aee4')])