我真的很喜欢剧情中的additional colormaps个‘密集’或‘冰’.然而,我目前正在使用matplotlib来处理我的大部分情节.

有没有一种方法可以在matplotlib的曲线图中使用pyploy色彩映射表?

当我拿色彩映射表‘ice’为例,我得到的唯一东西是RGB colored颜色 作为字符串

import plotly.express as px

px.colors.sequential.ice

这是刚回来的

['rgb(3, 5, 18)',
 'rgb(25, 25, 51)',
 'rgb(44, 42, 87)',
 'rgb(58, 60, 125)',
 'rgb(62, 83, 160)',
 'rgb(62, 109, 178)',
 'rgb(72, 134, 187)',
 'rgb(89, 159, 196)',
 'rgb(114, 184, 205)',
 'rgb(149, 207, 216)',
 'rgb(192, 229, 232)',
 'rgb(234, 252, 253)']

问题是,我不知道如何在matplotlib情节中使用它.我试过的东西是用custom colormap

my_cmap = matplotlib.colors.ListedColormap(px.colors.sequential.ice, name='my_colormap_name')

但当我在绘图中使用时,这给了我以下错误:

ValueError: Invalid RGBA argument: 'rgb(3, 5, 18)'

有人知道如何正确转换吗?

推荐答案

你必须解码RGB字符串:

import plotly.express as px
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors

SAMPLES = 20
ice = px.colors.sample_colorscale(px.colors.sequential.ice, SAMPLES)
rgb = [px.colors.unconvert_from_RGB_255(px.colors.unlabel_rgb(c)) for c in ice]

cmap = mcolors.ListedColormap(rgb, name='Ice', N=SAMPLES)

演示:

import numpy as np

gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))
plt.imshow(gradient, aspect='auto', cmap=cmap)
plt.show()

输出:

enter image description here

Python相关问答推荐

Python在通过Inbox调用时给出不同的响应

如何判断. text文件中的某个字符,然后读取该行

将嵌套列表的字典转换为数据框中的行

GEKKO:已知延迟的延迟系统的参数估计

如何将带有逗号分隔的数字的字符串解析为int Array?

如何使用上下文管理器创建类的实例?

如果索引不存在,pandas系列将通过索引获取值,并填充值

连接两个具有不同标题的收件箱

将输入管道传输到正在运行的Python脚本中

PMMLPipeline._ fit()需要2到3个位置参数,但给出了4个位置参数

Python虚拟环境的轻量级使用

Python中绕y轴曲线的旋转

如何使用Python以编程方式判断和检索Angular网站的动态内容?

在Python argparse包中添加formatter_class MetavarTypeHelpFormatter时, - help不再工作""""

根据列值添加时区

改进大型数据集的框架性能

调用decorator返回原始函数的输出

如果初始groupby找不到满足掩码条件的第一行,我如何更改groupby列,以找到它?

Python Tkinter为特定样式调整所有ttkbootstrap或ttk Button填充的大小,适用于所有主题

替换现有列名中的字符,而不创建新列