user14893379
user14893379

Reputation: 125

Multiple traces per animation frame in Plotly

I am currently trying to make an animation using Plotly's 3D-Scatterplots. I have so far successfully plotted several traces in a single figure, as well as created an animation using a single trace per frame, but I am unable to combine the two. Here's my code so far:

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)

Checking for the size of frames as well as frames[0].data shows that I have the correct number of frames as well as the correct number of traces set for data in a single frame. However, in the resulting plot, only the first frame with the first trace will show up and the animation won't start upon clicking Play. Any help would be appreciated.

Upvotes: 1

Views: 3247

Answers (1)

user14893379
user14893379

Reputation: 125

While the above code runs, plotly seems to plot only the amount of plots that is specified in the initialization of the go.Figure() object, even if you pass a larger amount of plots per frame into the frames argument. At least that's what I think is going on.

Here's the updated code:

...
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
...

This problem is also somewhat outlined in Plotly: Animating a variable number of traces in each frame in R , where dummy traces have to be present if a frame has fewer traces than are present in the initialization. The bottom line is, the length of the passed list matters.

Upvotes: 2

Related Questions