如何在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
相关产品推荐
相关产品推荐

