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

Plotly Express动画折线极坐标(雷达)图固定theta值问题

问题解决方法

可以固定theta轴的方向值位置,核心是手动指定全局统一的theta轴类目排序,避免Plotly根据单帧数据动态调整轴刻度。
动画帧切换时方向标识位置变动的原因:当前代码没有指定theta轴的固定排序,Plotly默认会根据每帧内实际出现的方向值自动调整轴的类目顺序和展示范围,导致不同帧的刻度位置发生偏移。

以下是修改后的可运行代码:

import numpy as np
import plotly.express as px

# 预先获取全量风向的固定顺序,基于原始数据集的完整取值
fixed_direction_order = px.data.wind()['direction'].unique().tolist()

df = px.data.wind()
df = df.iloc[1:]
df['frameId'] = np.random.randint(1, 21, df.shape[0])

fig = px.line_polar(df, r="frequency", theta="direction", line_close=True,
                    color_discrete_sequence=px.colors.sequential.Plasma_r, animation_frame="frameId")

# 核心配置:固定theta轴的类目顺序
fig.update_polars(
    thetaaxis=dict(
        categoryorder="array",
        categoryarray=fixed_direction_order
    )
)

fig.show()

关键参数说明

  • update_polars 用于统一设置极坐标的全局配置,不会随动画帧切换变动
  • thetaaxis.categoryorder 设为array代表使用自定义的类目排序规则
  • thetaaxis.categoryarray 传入你需要固定展示的所有方向值列表,列表顺序就是theta轴上方向标识的排列顺序,和原始数据集的默认风向顺序一致即可。

效果示例图

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 07:15:06