How to animate a heatmap in Plotly

大憨熊 提交于 2021-02-08 07:57:34

问题


The Plotly documentation on heatmap animation is pretty difficult to understand. Could someone explain how to animate a heatmap?


Here is my specific example.

import numpy as np
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
init_notebook_mode(connected=True)

x = np.linspace(0, 1, 200)
y = np.linspace(0, 1, 200)
xx, yy = np.meshgrid(x, y)

def plane_wave(phase):
    return np.sin(8*np.pi * (xx - phase))

zz = plane_wave(0)

trace = go.Heatmap(
    x=x,
    y=y,
    z=zz,
)
# This doesn't work:
frames = [
    {'data': [{'z': plane_wave(phase)}]} for phase in np.linspace(0, 2*np.pi)
]

data=[trace]
fig = go.Figure(data=data, frames=frames)
iplot(fig)

I get

PlotlyDictKeyError: 'z' is not allowed in 'scatter'
Path To Error: ['frames'][0]['data'][0]['z']

Here's a picture of the heatmap I'm trying to animate:


回答1:


I figured out the problem with what I'd done - I needed to specify 'type': 'heatmap'

frames = [
    {
        'data': [{
            'z': plane_wave(angle, phase),
            'type': 'heatmap'
        }]
    } for phase in np.linspace(0, 0.1, 30)
]

However, the jupyter notebook now gives this error

IOPub data rate exceeded. The notebook server will temporarily stop sending output to the client in order to avoid crashing it. To change this limit, set the config variable --NotebookApp.iopub_data_rate_limit.

I followed the instructions here to stop this error. However, this is highly inconvenient. Also, the step between each frame takes is a noticeable gap (i.e., the frame rate is low). I wonder if there is a better method for animating a heatmap



来源:https://stackoverflow.com/questions/45712242/how-to-animate-a-heatmap-in-plotly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!