将RGB代码列表转换为Matplotlib颜色图 [英] Convert list of rgb codes to matplotlib colormap

查看:82
本文介绍了将RGB代码列表转换为Matplotlib颜色图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 rgb 值列表,想从列表中创建一个服装颜色图.

I have a list of rgb values and would like to create a costum colormap from the list.

这些是示例值:

my_rgbs =['#ece804','#ece804','#e8e804','#e4e804','#e4e804','#e0e804','#dce804','#dce804','#d8e804','#d4e404','#d4e404','#d0e404','#cce404','#cce404','#c8e404','#c4e404','#c4e404','#c0e404','#bce404','#bce404','#b8e404','#b4e004','#b4e004','#b0e004','#ace004','#ace004','#a8e004','#a4e004','#a4e008','#a0e008','#9ce008','#9ce008','#98dc08','#94dc08','#94dc08','#90dc08','#8cdc08','#8cdc08','#88dc08','#84dc08','#84dc08','#80dc08','#7cdc08','#7cdc08','#78d808','#74d808','#70d808','#70d808','#6cd808','#68d808','#68d808','#64d808','#60d808','#60d808','#5cd808','#58d408','#58d408','#54d408','#50d408','#50d40c','#4cd40c','#48d40c','#48d40c','#44d40c','#40d40c','#40d40c','#3cd40c','#38d00c','#38d00c','#34d00c','#30d00c','#30d00c','#2cd00c','#28d00c','#28d00c','#24d00c','#20d00c','#20d00c','#1ccc0c','#18cc0c','#18cc0c','#14cc0c','#10cc0c','#10cc0c','#0ccc0c','#08cc0c','#08cc0c','#04cc0c','#00cc0c','#00cc0c','#08c81c','#10c428','#18c034','#20bc40','#28bc4c','#30b858','#38b464','#40b070','#48b07c','#44b878','#40bc78','#3cc074','#38c474','#34cc74','#30d070','#2cd470','#28d870','#24d474','#24d478','#20d47c','#20d07c','#1cd080','#1cd084','#1ccc88','#18cc88','#18cc8c','#14c890','#14c890','#14c894','#10c498','#10c49c','#0cc49c','#0cc0a0','#0cc0a4','#08c0a8','#08bca8','#04bcac','#04bcb0','#04bcb0','#08b4ac','#0caca8','#0ca4a8','#109ca4','#1094a4','#148ca0','#1484a0','#187c9c','#18749c','#1c6c98','#1c6498','#205c94','#205494','#244c90','#244490','#283c8c','#28348c','#2c2c88','#2c2888','#302484','#302484','#302480','#302480','#302480','#30247c','#30247c','#30247c','#302478','#302478','#302074','#302074','#302074','#302070','#302070','#302070','#34206c','#34206c','#342068','#342068','#342068','#341c64','#341c64','#341c64','#341c60','#341c60','#341c60','#341c5c','#341c5c','#341c58','#341c58','#341858','#341854','#381854','#381854','#381850','#381850','#38184c','#38184c','#38184c','#381848','#381848','#381448','#381444','#381444','#381444','#381440','#381440','#38143c','#38143c','#3c143c','#3c1438','#3c1038','#3c1038','#3c1034','#3c1034','#3c1030','#3c1030','#3c1030','#3c102c','#3c102c','#3c102c','#3c1028','#3c0c28','#3c0c28','#3c0c24','#3c0c24','#400c20','#400c20','#400c20','#400c1c','#400c1c','#400c1c','#400818','#400818','#400814','#400814','#400814','#400810','#400810','#400810','#40080c','#40080c','#40080c','#440c10','#441010','#441010','#441410','#441414','#481814','#481814','#481c14','#481c18','#482018','#4c2018','#4c2418','#4c241c','#4c281c','#4c2c1c','#502c1c','#503020','#503020','#503420','#503420','#543824','#543824','#543c24','#543c24']

我知道创建服装颜色图的唯一方法是从这里:http://matplotlib.org/examples/pylab_examples/custom_cmap.html

The only method I know to create a costum colormap is from here: http://matplotlib.org/examples/pylab_examples/custom_cmap.html

例如:

from pylab import *
r1 = 255. / 255.
g1 = 255. / 255.
b1 = 210. / 255.
r2 = 15. / 255.
g2 = 50. / 255.
b2 = 0. / 255.
cdict = {'red': ((0, r1, r1),
                 (1, r2, r2)),
         'green': ((0, g1, g1),
                   (1, g2, g2)),
         'blue':  ((0, b1, b1),
                   (1, b2, b2))}
my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap',cdict,256)

这种方法将每个rgb值分别转换为r,g和b值,然后再转换为这样的字典将非常不便.我想知道是否存在更容易解决此问题的解决方案?非常感谢!

This approach would be quite inconvenient converting each individual rgb value to separate r, g and b values and then to such a dictionary. I was wondering if there exists a much easier solution for this problem? Thank you very much!

推荐答案

您想使用 ListedColormap 正是为了这个目的而存在的:

You want to use ListedColormap which exists for exactly this purpose:

my_cmap = matplotlib.colors.ListedColormap(my_rgbs, name='my_colormap_name')

这篇关于将RGB代码列表转换为Matplotlib颜色图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆