Python matplotlib.colors 模块,to_hex() 实例源码
我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用matplotlib.colors.to_hex()。
def hue_map(self, value):
'''
:param value: number 0 to 1,map to hue 0.6 - 0 (blue to red,like heatmap)
more highter the value,more warm the color
:return: hex of rgb
'''
HUE_MAX = 0.6
hue = pow(1 - value, 2) * HUE_MAX
rgb = colors.hsv_to_rgb((hue, 1, 1))
hex = colors.to_hex(rgb)
return hex
def swap_colors(json_file_path):
'''
Switches out color ramp in Meta.json files.
Uses custom color ramp if provided and valid; otherwise falls back to nextstrain default colors.
N.B.: Modifies json in place and writes to original file path.
'''
j = json.load(open(json_file_path, 'r'))
color_options = j['color_options']
for k,v in color_options.items():
if 'color_map' in v:
categories, colors = zip(*v['color_map'])
## Use custom colors if provided AND present for all categories in the dataset
if custom_colors and all([category in custom_colors for category in categories]):
colors = [ custom_colors[category] for category in categories ]
## Expand the color palette if we have too many categories
elif len(categories) > len(default_colors):
from matplotlib.colors import LinearSegmentedColormap, to_hex
from numpy import linspace
expanded_cmap = LinearSegmentedColormap.from_list('expanded_cmap', default_colors[-1], N=len(categories))
discrete_colors = [expanded_cmap(i) for i in linspace(0,1,len(categories))]
colors = [to_hex(c).upper() for c in discrete_colors]
else: ## Falls back to default nextstrain colors
colors = default_colors[len(categories)] # based on how many categories are present; keeps original ordering
j['color_options'][k]['color_map'] = map(list, zip(categories, colors))
json.dump(j, open(json_file_path, 'w'), indent=1)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。