You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中使用十六进制颜色码修改Plotly图表配色方案

Plotly 自定义连续配色方案修改方法

你只需将原代码中color_continuous_scale参数的取值从内置色带字符串'RdBu',替换为你指定的三个十六进制颜色码组成的列表即可,Plotly会自动对列表内的颜色做插值生成连续渐变色阶。

修改后的完整代码如下:

import plotly.express as px
import numpy as np
df = px.data.gapminder().query("year == 2007")
fig = px.treemap(df, path=[px.Constant("world"), 'continent', 'country'], values='pop',
                  color='lifeExp', hover_data=['iso_alpha'],
                  color_continuous_scale=["#32a852", "#3261a8", "#a83259"],
                  color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop']))
fig.update_layout(margin = dict(t=50, l=25, r=25, b=25))
fig.show()

如果后续你需要调整颜色的渐变顺序,直接调换列表内三个颜色的位置即可。

内容的提问来源于stack exchange,提问作者codedancer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 10:12:00