matplotlib 如何从色彩Map表中提取十六进制颜色代码

iyfamqjs  于 2023-03-09  发布在  其他
关注(0)|答案(2)|浏览(176)

从下面的branca色彩Map表

import branca

color_map = branca.colormap.linear.PuRd_09.scale(0, 250)

colormap = color_map.to_step(index=[0, 10, 20, 50, 70, 90, 120, 200])

如何从上面的Branca颜色Map表中提取所有步骤(索引)的十六进制颜色?

wmtdaxz3

wmtdaxz31#

您可以在colormap.colors上使用matplotlib.colors.to_hex

from matplotlib.colors import to_hex

out = [to_hex(c) for c in colormap.colors]

# or
out = list(map(to_hex, colormap.colors))

输出:

['#f7f4f9', '#f0ebf4', '#e3d9eb', '#d0aad2', '#d084bf', '#e44199', '#67001f']
py49o6xq

py49o6xq2#

source code on GitHub显示to_step返回一个StepColormap对象。该对象(链接代码中的第500行)显示StepColormap具有一个colors属性。您应该能够通过这种方式索引颜色:

import branca

color_map = branca.colormap.linear.PuRd_09.scale(0, 250)

colormap = color_map.to_step(index=[0, 10, 20, 50, 70, 90, 120, 200])

for color in colormap.colors:
    print(color)

相关问题