Hi, I am trying to make a continuous line using plotly.py in Jupiter notebook, is there a function to do this?
I’m able to make a graph, but I need to input an equation so that the line is continuous and infinite, not limited to a certain number of points.
If I understand correctly you want a graph which keeps updating with more data or “continuous line”, although it its not possible with just the plotly.py library as the figure once generated can’t update automatically on its own. But it is possible with the Dash library, in Dash you can update the figures using callback functions.
Below is a simple example in Dash of what you maybe looking for:
import dash #pip install dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Output, Input
import plotly.express as px
import random
X = [0]
Y = [1]
app = dash.Dash(__name__)
#App Layout
app.layout = html.Div(children=[
html.H1(children='Dash is Awesome!'),
dcc.Graph(id='live-graph'),
dcc.Interval(
id='graph-update',
interval=1000
),
]
)
#App Callback
@app.callback(Output('live-graph', 'figure'),
[Input('graph-update', 'n_intervals')])
def update_graph(input_data):
X.append(X[-1]+1)
Y.append(Y[-1]+Y[-1]*random.uniform(-0.1,0.1))
# Figure
fig = px.line(x=list(X), y=list(Y))
fig.update_layout(xaxis=dict(range=[X[0],X[-1]]), yaxis=dict(range=[min(Y)-0.3,max(Y)+0.1]))
return fig
if __name__ == '__main__':
app.run_server(debug=True)
If you want to run it on a Jupyter Notebook:
Copy the entire code below in a single cell.
from jupyter_dash import JupyterDash #pip install jupyter-dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Output, Input
import plotly.express as px
import random
X = [0]
Y = [1]
app = JupyterDash(__name__)
#App Layout
app.layout = html.Div(children=[
html.H1(children='Dash is Awesome!'),
dcc.Graph(id='live-graph'),
dcc.Interval(
id='graph-update',
interval=1000
),
]
)
#App Callback
@app.callback(Output('live-graph', 'figure'),
[Input('graph-update', 'n_intervals')])
def update_graph(input_data):
X.append(X[-1]+1)
Y.append(Y[-1]+Y[-1]*random.uniform(-0.1,0.1))
# Figure
fig = px.line(x=list(X), y=list(Y))
fig.update_layout(xaxis=dict(range=[X[0],X[-1]]), yaxis=dict(range=[min(Y)-0.3,max(Y)+0.1]))
return fig
if __name__ == '__main__':
app.run_server(mode="inline")
1 Like
where you have
X = [0]
Y = [1]
would I input my own data?