Python 将值映射到matplotlib中的颜色
我有一个数字清单如下:Python 将值映射到matplotlib中的颜色,python,matplotlib,Python,Matplotlib,我有一个数字清单如下: lst = [1.9378076554115014, 1.2084586588892861, 1.2133096565896173, 1.2427632053442292, 1.1809971732733273, 0.91960143581348919, 1.1106310149587162, 1.1106310149587162, 1.1527004351293346, 0.87318084435885079, 1.166
lst = [1.9378076554115014, 1.2084586588892861, 1.2133096565896173,
1.2427632053442292, 1.1809971732733273, 0.91960143581348919,
1.1106310149587162, 1.1106310149587162, 1.1527004351293346,
0.87318084435885079, 1.1666132876686799, 1.1666132876686799]
我想将这些数字转换为显示的颜色。
我想要灰度,但当我按原样使用这些数字时,它会给我一个错误:
ValueError: to_rgba: Invalid rgba arg "1.35252299785"
to_rgb: Invalid rgb arg "1.35252299785"
gray (string) must be in range 0-1
…据我所知,这是因为它超过1
接下来,我尝试将列表中的项目与列表中的最高数字进行分割,以给出小于1的值。但这给出了一个非常窄的色阶,值之间几乎没有任何差异
有没有什么方法可以给颜色赋予一些最小和最大范围,并将这些值转换为颜色?我使用的是matplotlib。您需要的是
matplotlib.colors
模块。这提供了许多从值映射到颜色映射值的类
import matplotlib
import matplotlib.cm as cm
lst = [1.9378076554115014, 1.2084586588892861, 1.2133096565896173, 1.2427632053442292,
1.1809971732733273, 0.91960143581348919, 1.1106310149587162, 1.1106310149587162,
1.1527004351293346, 0.87318084435885079, 1.1666132876686799, 1.1666132876686799]
minima = min(lst)
maxima = max(lst)
norm = matplotlib.colors.Normalize(vmin=minima, vmax=maxima, clip=True)
mapper = cm.ScalarMappable(norm=norm, cmap=cm.Greys_r)
for v in lst:
print(mapper.to_rgba(v))
一般的方法是在数据中找到最小值
和最大值
。使用这些来创建一个标准化实例(其他标准化类可用,例如log scale)。接下来,使用Normalize
实例和您选择的参数创建一个ScalarMappable
。然后,您可以使用mapper.to_rgba(v)
通过标准化比例将输入值v
映射到目标颜色
for v in sorted(lst):
print("%.4f: %.4f" % (v, mapper.to_rgba(v)[0]) )
生成输出:
0.8732: 0.0000
0.9196: 0.0501
1.1106: 0.2842
1.1106: 0.2842
1.1527: 0.3348
1.1666: 0.3469
1.1666: 0.3469
1.1810: 0.3632
1.2085: 0.3875
1.2133: 0.3916
1.2428: 0.4200
1.9378: 1.0000
如果需要,matplotlib.colors
会提供更多信息。彩色贴图功能强大,但(a)您通常可以做一些更简单的事情,(b)因为它们功能强大,有时会比我预期的做得更多。扩展mfitzp的示例:
import matplotlib
import matplotlib.cm as cm
lst = [1.9378076554115014, 1.2084586588892861, 1.2133096565896173, 1.2427632053442292,
1.1809971732733273, 0.91960143581348919, 1.1106310149587162, 1.1106310149587162,
1.1527004351293346, 0.87318084435885079, 1.1666132876686799, 1.1666132876686799]
minima = min(lst)
maxima = max(lst)
norm = matplotlib.colors.Normalize(vmin=minima, vmax=maxima, clip=True)
mapper = cm.ScalarMappable(norm=norm, cmap=cm.Greys)
for v in lst:
print(mapper.to_rgba(v))
# really simple grayscale answer
algebra_list = [(x-minima)/(maxima-minima) for x in lst]
# let's compare the mapper and the algebra
mapper_list = [mapper.to_rgba(x)[0] for x in lst]
matplotlib.pyplot.plot(lst, mapper_list, color='red', label='ScalarMappable')
matplotlib.pyplot.plot(lst, algebra_list, color='blue', label='Algebra')
# I did not expect them to go in opposite directions. Also, interesting how
# Greys uses wider spacing for darker colors.
# You could use Greys_r (reversed)
# Also, you can do the colormapping in a call to scatter (for instance)
# it will do the normalizing itself
matplotlib.pyplot.scatter(lst, lst, c=lst, cmap=cm.Greys, label='Default norm, Greys')
matplotlib.pyplot.scatter(lst, [x-0.25 for x in lst], marker='s', c=lst,
cmap=cm.Greys_r, label='Reversed Greys, default norm')
matplotlib.pyplot.legend(bbox_to_anchor=(0.5, 1.05))
我注意到我的回答中有一个错误:您需要使用cm.Greys\u r
(反灰色)颜色映射来显示数据。