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

Plotly极坐标柱状图Barpolar自定义theta轴单位为24小时制时间的实现问题

解决方案

你需要给angularaxis配置tickvals(指定刻度显示的角度位置)和ticktext(指定每个位置对应的显示文本)即可实现需求。你之前配置的categoryorder、categoryarray是分类轴生效的参数,而你将角轴类型设为了linear,因此这两个参数不会生效。

修改后的完整代码如下:

import plotly.graph_objects as go

fig = go.Figure(go.Barpolar(
    r= bins['justtime'],
    theta= [x*15 for x in bins['index']],
    width= [14 for x in bins['index']],
    marker_line_color="black",
    marker_line_width=2,
    opacity=0.8
))

fig.update_layout(
    template=None,
    polar = dict(
        radialaxis = dict(range=[0, bins['justtime'].max()], showticklabels=False, ticks=''),
        angularaxis = dict(
            showticklabels=True,
            type='linear',
            # 24小时对应每15°一个刻度位置
            tickvals = [i*15 for i in range(24)],
            # 每个刻度位置对应显示的小时文本
            ticktext = [f"{i:02d}:00" for i in range(24)],
            # 可选配置:如果需要00:00显示在极坐标顶部,取消注释下一行
            # rotation = 90
        )
    )
)

fig.show()

如果你不需要显示:00后缀,直接把ticktext替换为[str(i) for i in range(24)]即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 00:30:02