我有一个20个值的Pandas系列,我正在尝试使用自定义颜色图绘制图表,但是每个值都返回相同的颜色,尽管它们是唯一的。我已经在更广泛的范围内使用了相同的代码,并且可以正常工作,看来数字是如此接近是问题所在。
#my array
print(combined.values)
print(type(combined.values))
[4.58019608 4.4845098 4.4818 4.4288 4.4166 4.40807692
4.3688 4.359 4.3446 4.3318 3.6424 3.6306
3.6248 3.6194 3.617 3.6152 3.5628 3.4948
3.4424 3.3692 ]
<class 'numpy.ndarray'>
cmap = mcolors.LinearSegmentedColormap.from_list("", ['white', 'black'])
#try to pass values to cmap
print(cmap(combined.values))
[[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 1.]]
如何标准化颜色图的这些值?理想情况下,最小值应在开头,最大值应在结尾。
答案 0 :(得分:2)
似乎matplotlib颜色图要求传递的值介于0和1之间。将数据数组规范化为介于0和1之间(含0和1)(包括“ MinMaxScaling”),似乎会产生(r,g,b,a)代表有用的灰度渐变的值。
c = combined.values.copy()
c = (c - c.min()) / (c.max() - c.min())
cmap = mcolors.LinearSegmentedColormap.from_list("", ['white', 'black'])
print(cmap(c))
[[0. 0. 0. 1. ]
[0.07843137 0.07843137 0.07843137 1. ]
[0.07843137 0.07843137 0.07843137 1. ]
[0.1254902 0.1254902 0.1254902 1. ]
[0.13333333 0.13333333 0.13333333 1. ]
[0.14117647 0.14117647 0.14117647 1. ]
[0.17254902 0.17254902 0.17254902 1. ]
[0.18039216 0.18039216 0.18039216 1. ]
[0.19215686 0.19215686 0.19215686 1. ]
[0.20392157 0.20392157 0.20392157 1. ]
[0.77647059 0.77647059 0.77647059 1. ]
[0.78431373 0.78431373 0.78431373 1. ]
[0.78823529 0.78823529 0.78823529 1. ]
[0.79607843 0.79607843 0.79607843 1. ]
[0.79607843 0.79607843 0.79607843 1. ]
[0.79607843 0.79607843 0.79607843 1. ]
[0.84313725 0.84313725 0.84313725 1. ]
[0.89803922 0.89803922 0.89803922 1. ]
[0.94117647 0.94117647 0.94117647 1. ]
[1. 1. 1. 1. ]]