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

如何用Python Plotly实现Numpy二维数组的热力图同页动画?

解决Plotly热力图动画多标签页问题

你的问题核心是循环中重复调用fig.show()且帧生成逻辑错误,导致每个帧单独打开标签页。以下是修正方案:

问题分析

  1. 重复调用fig.show():每次调用都会在新标签页渲染单独的图,而非累积成动画。
  2. 帧数据逻辑错误:原代码中final_therm[i]试图取当前帧数组的行数据,但每个动画帧应该是完整的24x32热力图数组。
  3. 循环重复创建Figure:没有预先收集所有帧数据,而是每次循环重建Figure,无法形成连贯动画。

修正后的代码

import numpy as np
import plotly.graph_objects as go

# 1. 先收集所有时间点的热力图数据
frames_data = []
for j in range(2, len(thermal_np)):
    y = np.array(thermal_np[j])
    final_therm = y.reshape(24, 32)
    frames_data.append(final_therm)

# 2. 创建带动画的Figure
fig = go.Figure(
    # 初始化第一帧数据
    data=go.Heatmap(z=frames_data[0], colorscale='jet', showscale=True, zsmooth='best'),
    layout=go.Layout(
        margin=dict(t=200, r=200, b=200, l=200),
        showlegend=False,
        width=1000, height=1000,
        autosize=True,
        # 添加动画控制按钮(播放/暂停)
        updatemenus=[{
            'type': 'buttons',
            'buttons': [
                {
                    'label': '播放',
                    'method': 'animate',
                    'args': [None, {'frame': {'duration': 1000, 'redraw': True}, 'fromcurrent': True}]
                },
                {
                    'label': '暂停',
                    'method': 'animate',
                    'args': [[None], {'frame': {'duration': 0, 'redraw': False}, 'mode': 'immediate'}]
                }
            ],
            'direction': 'left',
            'pad': {'r': 10, 't': 87},
            'showactive': True,
            'x': 0.1,
            'xanchor': 'right',
            'y': 0,
            'yanchor': 'top'
        }]
    ),
    # 生成所有动画帧
    frames=[go.Frame(
        data=[go.Heatmap(z=frame_data, colorscale='jet', showscale=True, zsmooth='best')],
        layout=go.Layout(title_text=f"Frame {idx+1}")
    ) for idx, frame_data in enumerate(frames_data)]
)

# 设置初始标题
fig.update_layout(title_text=f"Frame 1")

# 仅调用一次show,在同一标签页展示完整动画
fig.show()

关键改进点

  • 预收集帧数据:先遍历所有传感器数据,把每个时间点的24x32热力图数组存入列表,确保所有动画帧数据完整。
  • 单Figure+多Frames:基于所有帧数据创建单个Figure,通过frames参数定义每个动画帧的内容。
  • 动画控制按钮:添加播放/暂停按钮,自定义帧间隔(duration=1000对应1秒每帧,替代原代码的time.sleep(1))。
  • 单次fig.show():仅调用一次展示方法,所有帧在同一标签页以动画形式播放。

如果你的数据是实时流(传感器持续输出),可以改用Plotly的FigureWidget实现实时更新,无需预收集数据,但上述方案适用于离线处理已有时间序列数据的场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 01:20:36