question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Underlying plot data dissapears when animation frames are added

See original GitHub issue

Hello folks, I’m posting this issue as I couldn’t find answer on stackoverflow and in the docs. I’m trying to create animated plot with some data underneath, and animate points in consecutive frames. When I add the frames to the plot my underlying spline dissapears. I’m attaching exemplary script which you can paste into jupyter notebook.

Python 3.7.5

jupyter==1.0.0
numpy==1.18.1
plotly==4.5.4
import math
import numpy as np
import plotly.graph_objects as go
import plotly.io as pio
import plotly.offline as pyo


class Plot:

    def __init__(
        self,
        image_width: int = 1200,
        image_height: int = 900
    ) -> None:

        self.IMAGE_WIDTH = image_width
        self.IMAGE_HEIGHT = image_height

        pyo.init_notebook_mode(connected=False)
        pio.renderers.default = 'notebook'

    def population(self, filename: str = 'population'):
        x = np.linspace(
            start=0,
            stop=20,
            num=100,
            endpoint=True
        )

        y = np.array([math.sin(x_i) for x_i in x])

        x_min = np.min(x)
        x_max = np.max(x)

        y_min = np.min(y)
        y_max = np.max(y)

        fig = go.Figure(
            data=[
                go.Scatter(
                    y=y,
                    x=x,
                    mode="lines",
                    line_shape='spline'
                )
            ]
        )

        frames = []

        for i in range(50):
            x = np.random.random_sample(size=5)
            x *= 20

            y = np.array([math.sin(x_i) for x_i in x])

            scatter = go.Scatter(
                x=x,
                y=y,
                mode='markers',
                marker=dict(
                    color='Green',
                    size=12,
                    line=dict(
                        color='Red',
                        width=2
                    )
                ),
            )

            frame = go.Frame(data=[scatter])

            frames.append(frame)

        fig.frames = frames

        fig.layout = go.Layout(
            xaxis=dict(
                range=[x_min, x_max],
                autorange=False
            ),
            yaxis=dict(
                range=[y_min, y_max],
                autorange=False
            ),
            title="Start Title",
            updatemenus=[
                dict(
                    type="buttons",
                    buttons=[
                        dict(
                            label="Play",
                            method="animate",
                            args=[None]
                        )
                    ]
                )
            ]
        )

        fig.update_layout(
            xaxis_title='x',
            yaxis_title='y',
            title='Fitness landscape',
            # autosize=True
        )

        pyo.iplot(
            fig,
            filename=filename,
            image_width=self.IMAGE_WIDTH,
            image_height=self.IMAGE_HEIGHT
        )
plot = Plot()
plot.population()

Without frames

obraz

With frames

Consecutive frames are missing underlying spline.

obraz

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
empetcommented, Apr 30, 2020

When your fig.data contains only one trace then it is supposed that each frame updates that trace and the trace is no more displayed during the animation.

That’s why you must define:

fig = go.Figure(
            data=[
                go.Scatter(
                    y=y,
                    x=x,
                    mode="lines",
                    line_shape='spline'
                )]*2)

i.e. include the same trace twice in fig.data.

At the same time modify the frame definition as follows:

 frame = go.Frame(data=[scatter],
                  traces=[1])

traces = [1] informs plotly.js that each frame updates the trace fig.data[1], while fig.data[0] is unchanged during the animation.

1reaction
dtoniolocommented, Mar 11, 2021

Hi, can you give us additional clarifications on how go.Traces work? To be more specific, my current understanding is the following;

  • the frames argument in the go.Figure constructor is just a container for the frames. To define an animations, controls need to be added that tell the figure which frames to pick and in which order.
  • Each frame modifies certain traces. Which ones will be affected is specified by the traces argument. If it is not given, the traces are chosen as the first ones in the data argument of the figure.
  • The data argument of the figure is used to determine the traces that exist before the animation is started.

However, I have the following questions:

  • Instead of using the traces argument of the go.Frame constructor, can we pass named traces to the data argument (of the same constructor)?
  • Can the frames be used to define new traces (i.e. traces that don’t exist in the data argument of the figure) (e.g. if the figure only has one trace, but each frame as two)?
  • How can (if it is possible) data be shared across traces? I guess that the baseframe argument has something to do with this, but I’m not sure about its meaning.

I’ll post it here because in the end this issue seems to be about the current lack of good documentation about the inner workings of go.Frame, but I can open a new issue if needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dash Plotly Graph Lines Disappear in Certain Frames of ...
Animations are designed to work well when each row of input is present across all animation frames, and when categorical values mapped to...
Read more >
Building an animation step-by-step with gganimate
Animations show subsets of a dataset at different times, so I like to start by showing everything – all the data – in...
Read more >
gganimate (with a spooky twist) - Katherine Goode
gganimate provides "a range of new grammar classes that can be added to the plot ... defines how the data should appear and...
Read more >
Create timeline animations in Adobe Photoshop
Learn how to create timeline animations in Adobe Photoshop. ... Photoshop automatically adds or modifies a series of frames between two ...
Read more >
Animation configuration options in plotly - Rdrr.io
Animations can be created by either using the frame argument in plot_ly() or the (unofficial) frame ggplot2 aesthetic in ggplotly() .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found