
我真的很喜欢plotly 中的附加色彩图,例如“密集”或“冰”。尽管如此,我目前的大部分绘图都使用 matplotlib。
有没有办法在 matplotlib 图中使用 pyplot 颜色图?
当我以颜色图“ice”为例时,我得到的唯一结果是 rgb 颜色作为字符串
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 图中使用它。我尝试的事情是创建自定义颜色图
my_cmap = matplotlib.colors.listedcolormap(px.colors.sequential.ice, name='my_colormap_name')
但是在绘图中使用时这给了我以下错误:
2088shop商城购物系统是商城系统中功能最全的一个版本:非会员购物、商品无限级分类、不限商品数量、商品多级会员定价、上货库存、Word在线编辑器、订单详情销售报表、商品评论、留言簿、管理员多级别、VIP积分、会员注册积分奖励、智能新闻发布、滚动公告、投票调查、背景图片颜色更换、店标上传、版权联系方式修改、背景音乐(好歌不断)、广告图片支持Flash、弹出浮动广告、搜索引擎关健词优化、图文友情联
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()
输出:









