I want to create an animation using plotly library. I need a randomly moving point (from [0,0]) which leaves a trace behing.
Here you can see a video of what I need. - https://i.sstatic.net/mzxeY.jpg
-
2Welcome to Stack Overflow! Please take the tour, read what's on-topic here and How to Ask, and condense your code down to a minimal reproducible example. We ask for a MRE because (a) it's a waste of time to read through lines after lines of irrelevant code looking for what's wrong, and (b) often, the exercise of creating a MRE helps you isolate and identify the problem and helps you fix the issue yourself. How to debug small programs.pho– pho2020-09-24 16:17:50 +00:00Commented Sep 24, 2020 at 16:17
-
2"It doesn't work" is a bad way to ask for help. What doesn't work? How does it break? Do you get an error? Does the program run fine but not give you the output you want? Does it do anything at all? You should be doing these debugging tasks before you decide to ask on Stack Overflow so that you can include the details in your question and make it easier for people to help you.pho– pho2020-09-24 16:19:45 +00:00Commented Sep 24, 2020 at 16:19
-
1@SamV Welcome to the forum! Please take a closer look at How to Ask a question and how to provide a reproducble example. As it now stands, your question does not meet most of the desired SO standards. But since you're new, and since we're all friendly in here, I'll see if I can provide you with a basic plotly Dash app to get you started.vestland– vestland2020-09-24 22:42:33 +00:00Commented Sep 24, 2020 at 22:42
-
@SamV How did my suggestion work out for you?vestland– vestland2020-09-28 09:35:49 +00:00Commented Sep 28, 2020 at 9:35
Add a comment
|
1 Answer
The code snippet below will produce a Plotly Dash app that animates a form of Random Walk. How that walk will appear will depend entirely on what kind of random nunbers you base it on. Here I'm using two accumulated processes for the X and Y axes based on np.random.uniform. But you can easily change that to whatever random process you prefer.
Dash App after 2500 runs
Dash App after 5000 runs
Complete code:
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from jupyter_dash import JupyterDash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
# data
np.random.seed(4)
Y = np.random.randint(low=-1, high=2, size=1).tolist()
X = np.random.randint(low=-1, high=2, size=1).tolist()
df = pd.DataFrame({'X':X, 'Y':Y}, columns = ['X', 'Y'])
df.iloc[0]=0
# plotly app
app = JupyterDash(__name__)
app.layout = html.Div([
html.H1("Random stumble"),
dcc.Interval(
id='interval-component',
interval=1*1000, # in milliseconds
),
dcc.Graph(id='graph'),
])
# Define callback to update graph
@app.callback(
Output('graph', 'figure'),
[Input('interval-component', "n_intervals")]
)
def streamFig(value):
global df
Y = np.random.randint(low=-1, high=2, size=1).tolist()
X = np.random.randint(low=-1, high=2, size=1).tolist()
df2 = pd.DataFrame({'X':X, 'Y':Y}, columns = ['X', 'Y'])
df = df.append(df2, ignore_index=True)#.reset_index()
df3=df.copy()
df3=df3.cumsum()
fig = go.Figure(data=go.Scatter(x=df3['X'], y=df3['Y']))
fig.update_layout(title = dict(text = 'x =' + str(X) + ', y ='+str(Y) + ', len = '+str(len(df))))
#fig.update_layout(xaxis=dict(range=[-10,10]))
#fig.update_layout(yaxis=dict(range=[-10,10]))
return(fig)
app.run_server(mode='external', port = 8019, dev_tools_ui=True, debug=True,
dev_tools_hot_reload =True, threaded=False)

