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

如何在Plotly中调整色条标题的旋转角度?

如何旋转Plotly热力图的色条标题?

以下是取自Plotly官方文档的热力图代码:

import plotly.express as px
data=[[1, 25, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, 5, 20]]
fig = px.imshow(data,
                labels=dict(x="Day of Week", y="Time of Day", color="Productivity"),
                x=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
                y=['Morning', 'Afternoon', 'Evening']
               )
fig.update_xaxes(side="top")
fig.show()

运行上述代码会生成如下热力图:
生成的热力图

现在需要将色条标题“Productivity(生产力)”旋转为如下竖排样式(文字从下到上排列):
目标样式的色条标题

解决方法

只需在代码中添加色条标题的定向配置,即可实现目标效果:

import plotly.express as px
data=[[1, 25, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, 5, 20]]
fig = px.imshow(data,
                labels=dict(x="Day of Week", y="Time of Day", color="Productivity"),
                x=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
                y=['Morning', 'Afternoon', 'Evening']
               )
fig.update_xaxes(side="top")
# 配置色条标题的旋转与位置
fig.update_coloraxes(colorbar_title=dict(
    text="Productivity",
    orientation="v",
    yanchor="bottom",
    y=1,
    x=1.05
))
fig.show()

如果需要中文标题,直接将text的值改为“生产力”即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 09:27:19