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

Plotly 3D图表实时更新异常:代码无法自动更新需手动刷新

解决Dash中3D图表无法实时自动更新的问题

问题根源

你的代码存在两个核心问题:

  1. 全局变量的状态冲突:全局的X,Y,Z列表和迭代器a在Dash的debug模式下,会因代码自动重载被重复初始化;即使关闭debug,多进程环境下也会出现状态不一致,导致回调无法正确累积数据。
  2. 3D动画参数不兼容:animate=True是针对2D图表的平滑过渡设计,3D图表不支持该参数,且未设置动态坐标轴范围时,新数据可能超出视图范围,看起来像未更新。

修复后的代码

from dash import Dash, html, dcc, Input, Output
import plotly.graph_objects as go

class MyIterator:
    def __iter__(self):
        x, y, z = 1, 2, 3
        while True:
            # 替换为你的真实数据生成逻辑
            x += 0.5
            y += 0.3
            z += 0.7
            yield (x, y, z)

app = Dash(__name__)
app.layout = html.Div(
    [
        dcc.Graph(id='live-graph'),
        dcc.Interval(
            id='graph-update',
            interval=1000,
            n_intervals=0
        ),
    ]
)

@app.callback(
    Output('live-graph', 'figure'),
    Input('graph-update', 'n_intervals'),
    State('live-graph', 'figure')
)
def update_graph_scatter(n, current_figure):
    # 初始化或获取历史数据
    if current_figure is None or not current_figure['data']:
        X, Y, Z = [1], [2], [3]
    else:
        X = current_figure['data'][0]['x'].copy()
        Y = current_figure['data'][0]['y'].copy()
        Z = current_figure['data'][0]['z'].copy()
    
    # 初始化迭代器(仅第一次回调时创建)
    if not hasattr(update_graph_scatter, 'iterator'):
        update_graph_scatter.iterator = iter(MyIterator())
    XYZ = next(update_graph_scatter.iterator)
    
    # 追加新数据
    X.append(XYZ[0])
    Y.append(XYZ[1])
    Z.append(XYZ[2])

    # 构建3D线图数据
    data = [go.Scatter3d(x=X, y=Y, z=Z, mode='lines')]
    
    # 设置自适应坐标轴范围,确保所有数据在视图内
    layout = go.Layout(
        scene=dict(
            xaxis=dict(range=[min(X), max(X)]),
            yaxis=dict(range=[min(Y), max(Y)]),
            zaxis=dict(range=[min(Z), max(Z)]),
        ),
        margin=dict(l=0, r=0, b=0, t=0)
    )

    return {'data': data, 'layout': layout}

if __name__ == '__main__':
    app.run_server(debug=True)

关键修改说明

  • 替换全局状态存储:用回调函数的属性保存迭代器,通过State获取当前图表的历史数据,避免全局变量导致的状态混乱。
  • 移除无效动画参数:删掉animate=True,改为手动设置坐标轴动态范围,确保每次更新后视图能覆盖所有数据。
  • 数据状态闭环管理:依赖当前图表的状态传递历史数据,保证每次回调都基于最新的累积结果更新。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 16:55:36