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

Plotly添加3D注释后轨迹与坐标轴消失问题求助

问题描述

运行下方代码时,原本能正常显示3D图形,但取消注释fig.update_layout(scene=dict(annotations=annos),)添加注释后,轨迹和坐标轴全部消失,图形显示异常。

import plotly.graph_objs as go

x_coor=[21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6, 21.6,
21.6, 21.6, 21.6]
y_coor=[0.0, 0.0, None, 70.0, 70.0, None, 0.0, 3.984799575740227, None, 3.984799575740227, 11.091689619072922, None, 11.091689619072922, 19.061288770553375, None, 19.061288770553375, 27.030887922033827, None, 27.030887922033827, 35.0, None, 70.0, 66.01520042425977, None, 66.01520042425977, 58.90831038092708, None, 58.90831038092708, 50.938711229446625, None, 50.938711229446625, 42.96911207796617, None, 42.96911207796617, 35.0, None, 35.0, 35.0, None, 82.0, 82.0, None, 82.0, 77.89068291331529, None, 77.89068291331529, 70.0, None]
z_coor=[0.0, 8.0, None, 0.0, 8.0, None, 8.0, 8.348385334336147, None, 8.348385334336147, 8.969730578124661, None, 8.969730578124661, 9.666501246796951, None, 9.666501246796951, 10.363271915469245, None, 10.363271915469245, 11.06, None, 8.0, 8.348385334336147, None, 8.348385334336147, 8.969730578124661, None, 8.969730578124661, 9.666501246796951, None, 9.666501246796951, 10.363271915469245, None, 10.363271915469245, 11.06, None, 0.0, 11.06, None, 0.0, 5.0, None, 5.0, 5.684886181114119, None, 5.684886181114119, 7.0, None]

annos=[{'x': 21.6, 'y': 0.0, 'z': 4.0, 'text': 'BB_350x200x8: HA350'}, {'x': 21.6, 'y': 70.0, 'z': 4.0, 'text': 'BB_350x200x8: HA350'}, {'x': 21.6, 'y': 1.9923997878701134, 'z': 8.174192667168073, 'text': 'BB_1100x200x8: HA350'}, {'x': 21.600000000000005, 'y': 7.538244597406575, 'z': 8.659057956230404, 'text': 'BB_800x200x8: HA350'}, {'x': 21.6, 'y': 15.07648919481315, 'z': 9.318115912460806, 'text': 'BB_800x200x8: HA350'}, {'x': 21.6, 'y': 23.0460883462936, 'z': 10.014886581133098, 'text': 'BB_800x200x8: HA350'}, {'x': 21.6, 'y': 31.015443961016913, 'z': 10.711635957734622, 'text': 'BB_800x200x8: HA350'}, {'x': 21.6, 'y': 68.00760021212989, 'z': 8.174192667168073, 'text': 'BB_800x200x8: HA350'}, {'x': 21.600000000000005, 'y': 62.461755402593425, 'z': 8.659057956230404, 'text': 'BB_800x200x8: HA350'}, {'x':21.6, 'y': 54.92351080518685, 'z': 9.318115912460806, 'text': 'BB_800x200x8: HA350'}, {'x': 21.6, 'y': 46.9539116537064, 'z': 10.014886581133098, 'text': 'BB_800x200x8: HA350'}, {'x': 21.6, 'y': 38.98455603898309, 'z': 10.711635957734622, 'text': 'BB_800x200x8: HA350'},{'x': 21.6, 'y': 35.0, 'z': 5.53, 'text': 'BB_450x150x6: HA350'}, {'x': 21.6, 'y': 82.0, 'z': 2.5, 'text': 'BB_300x150x6: HA350'}, {'x': 21.6, 'y': 79.94534145665764, 'z': 5.34244309055706, 'text': 'BB_900x150x8: HA350'}, {'x': 21.6, 'y': 73.94534145665764, 'z': 6.34244309055706, 'text': 'BB_900x150x8: HA350'}]

x_coor = [x_coor[0]]*len(x_coor)

trace_members = go.Scatter3d(
    x=x_coor,
    y=y_coor,
    z=z_coor,
    mode='lines+text', # draw 'line'
    name='members', # legend name
    line=dict(
        color='black',
        width=8,
        dash='solid',
    ),
)

fig = go.Figure(data=[trace_members])
#  fig.update_layout(scene=dict(annotations=annos),)
camera = dict(
  up=dict(x=0, y=0, z=1),
  center=dict(x=0, y=0, z=0),
  eye=dict(x=-2, y=0, z=0) # project to yz plane
)
fig.update_layout(scene_camera=camera)
fig.show()
解决方案

问题出在3D注释的配置上:直接将annos赋值给scene.annotations时,每个注释元素缺少坐标系引用(xref='x3d'、yref='y3d'、zref='z3d'),导致Plotly无法正确识别注释的空间位置,进而破坏了整个场景的渲染。

有两种修复方式:

方式一:补全注释的坐标系引用

修改annos列表,给每个注释字典添加必要的引用字段:

import plotly.graph_objs as go

# 原数据定义不变...

# 补全每个注释的坐标系引用
annos = [
    {**anno, 'xref': 'x3d', 'yref': 'y3d', 'zref': 'z3d'}
    for anno in annos
]

fig = go.Figure(data=[trace_members])
fig.update_layout(scene=dict(annotations=annos),)
camera = dict(
  up=dict(x=0, y=0, z=1),
  center=dict(x=0, y=0, z=0),
  eye=dict(x=-2, y=0, z=0) # project to yz plane
)
fig.update_layout(scene_camera=camera)
fig.show()

方式二:使用fig.add_annotation()逐个添加注释

这种方式会自动补全坐标系引用,代码更简洁:

import plotly.graph_objs as go

# 原数据定义不变...

fig = go.Figure(data=[trace_members])

# 循环添加每个注释
for anno in annos:
    fig.add_annotation(**anno, xref='x3d', yref='y3d', zref='z3d')

camera = dict(
  up=dict(x=0, y=0, z=1),
  center=dict(x=0, y=0, z=0),
  eye=dict(x=-2, y=0, z=0) # project to yz plane
)
fig.update_layout(scene_camera=camera)
fig.show()

两种方式都能让注释正常显示,同时保留原有的轨迹和坐标轴。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 22:39:53