Plotly 中每个动画帧有多个轨迹

Multiple traces per animation frame in Plotly

我目前正在尝试使用 Plotly 的 3D 散点图制作动画。到目前为止,我已经成功地在一个图中绘制了多条轨迹,并且每帧使用一条轨迹创建了一个动画,但我无法将两者结合起来。到目前为止,这是我的代码:

def animationTest(frames):
    # Defining frames
    frames=[
            go.Frame(
                data=[
                    go.Scatter3d(
                        x=trace.x,
                        y=trace.y,
                        z=trace.z,
                        line=dict(width=2)
                    )
                for trace in frame],
            )
        for frame in frames]
    # Defining figure
    fig = go.Figure(
        data=[
            go.Scatter3d(
            )
        ],
        layout=go.Layout( # Styling
            scene=dict(
            ),
            updatemenus=[
                dict(
                    type='buttons',
                    buttons=[
                        dict(
                            label='Play',
                            method='animate',
                            args=[None]
                        )
                    ]
                )
            ]
        ),
        frames=frames
    )
    fig.show()

class TestTrace():
    # 3D test data to test the animation
    def __init__(self,ind):
        self.x = np.linspace(-5.,5.,11)
        self.y = ind*np.linspace(-5.,5.,11) # ind will change the slope
        self.z = np.linspace(-5.,5.11)

fps = np.linspace(1.,10.,11)
inds = [-2,2]

#Slope changes for each frame for each trace so there's something to animate
frames = [[TestTrace(ind=f*ind) for ind in inds] for f in fps]

animationTest(frames)

检查 framesframes[0].data 的大小显示我有正确的帧数以及为 data 设置的正确的跟踪数框架。但是,在生成的图中,只会显示具有第一条轨迹的第一帧,并且动画不会在单击“播放”时开始。任何帮助将不胜感激。

当上面的代码运行时,plotly 似乎只绘制在 go.Figure() 对象的初始化中指定的绘图数量,即使您将每帧的大量绘图传递给框架参数。至少我是这么认为的。

这是更新后的代码:

...
fig = go.Figure(
    data=[
        go.Scatter3d(
        )
    for traces in frames[0]], # This has to match the number of plots that will be passed to frames
...

此问题在 Plotly: Animating a variable number of traces in each frame in R 中也有所概述,如果帧的轨迹少于初始化中存在的轨迹,则必须存在虚拟轨迹。最重要的是,传递的列表的长度很重要。