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

在Plotly中如何将色阶与对应色阶标题居中对齐?

当前色阶效果图

这个需求可以实现。你之前调整的colorbar_xanchor、colorbar_x参数用于控制色条整体(标题+色阶块)在画布上的水平位置,不会影响色阶块和其标题的内部对齐关系。

解决方法

核心是配置色条标题的align参数为center,即可实现标题和对应色阶块的居中对齐,也可搭配titlepad参数调整标题与色阶块的间距,优化显示效果。

Plotly Express 示例代码

import plotly.express as px

# 示例数据
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="petal_length")

# 调整色条对齐属性
fig.update_coloraxes(
    colorbar=dict(
        title=dict(
            text="花瓣长度",
            align="center" # 核心居中对齐参数
        ),
        titlepad=10 # 可选:调整标题和色阶块的间距
    )
)

fig.show()

Plotly Graph Objects 示例代码

import plotly.graph_objects as go

fig = go.Figure(
    data=go.Heatmap(
        z=[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
        colorbar=dict(
            title=dict(
                text="数值大小",
                align="center"
            ),
            titlepad=10
        )
    )
)

fig.show()

如果你的色条是水平放置(配置了orientation="h"),上述参数同样生效,可直接实现标题和色阶的居中对齐。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 11:48:02