如何在Plotly

时间:2017-08-16 11:23:19

标签: python animation plotly heatmap

Plotly documentation on heatmap animation很难理解。有人可以解释如何为热图设置动画吗?

这是我的具体例子。

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)

我得到了

  

PlotlyDictKeyError: 'z' is not allowed in 'scatter' {
{1}}

这是我试图制作动画的热图的图片:

Plane wave heatmap

1 个答案:

答案 0 :(得分:1)

我弄清楚我所做的事情 - 我需要指定'type': 'heatmap'

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

但是,jupyter笔记本现在出现此错误

  

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.

我按照说明here停止了此错误。然而,这非常不方便。而且,每帧所花费的步骤是明显的间隙(即帧速率低)。我想知道是否有更好的方法来设置热图的动画