Skip to content

Underlying plot data dissapears when animation frames are added #2423

Closed
@piotr-rarus

Description

@piotr-rarus

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions