Underlying plot data dissapears when animation frames are added
See original GitHub issueHello 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
With frames
Consecutive frames are missing underlying spline.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
i.e. include the same trace twice in
fig.data
.At the same time modify the frame definition as follows:
traces = [1]
informs plotly.js that each frame updates the tracefig.data[1]
, whilefig.data[0]
is unchanged during the animation.Hi, can you give us additional clarifications on how
go.Trace
s work? To be more specific, my current understanding is the following;frames
argument in thego.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.traces
argument. If it is not given, the traces are chosen as the first ones in thedata
argument of the figure.data
argument of the figure is used to determine the traces that exist before the animation is started.However, I have the following questions:
traces
argument of thego.Frame
constructor, can we pass named traces to thedata
argument (of the same constructor)?data
argument of the figure) (e.g. if the figure only has one trace, but each frame as two)?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.