I want to track some data in real time and let the user select their ranges when interacting with charts. I created this small example (I got it from the tutorial), and the problem is that every time I update the plot, everything becomes reset, since it update_graph_live()returns a new Plotly figure. (see example below)
Is it possible to update only the data, so the figure does not restart, and reset - by default / settings? I used to use d3.js and send data via websockets, so I could filter the data in the browser. But I would like to do this with Dash directly.
import dash
from dash.dependencies import Output, Event
import dash_core_components as dcc
import dash_html_components as html
from random import random
import plotly
app = dash.Dash(__name__)
app.layout = html.Div(
html.Div([
html.H4('Example'),
dcc.Graph(id='live-update-graph'),
dcc.Interval(
id='interval-component',
interval=1*1000
)
])
)
@app.callback(Output('live-update-graph', 'figure'),
events=[Event('interval-component', 'interval')])
def update_graph_live():
fig = plotly.tools.make_subplots(rows=2, cols=1, vertical_spacing=0.2)
fig['layout']['margin'] = {
'l': 30, 'r': 10, 'b': 30, 't': 10
}
fig['layout']['legend'] = {'x': 0, 'y': 1, 'xanchor': 'left'}
fig.append_trace({
'x': [1, 2, 3, 4, 5],
'y': [random() for i in range(5)],
'name': 'Foo',
'mode': 'lines+markers',
'type': 'scatter'
}, 1, 1)
fig.append_trace({
'x': [1, 2, 3, 4, 5],
'y': [random() for i in range(5)],
'name': 'Bar',
'type': 'bar'
}, 2, 1)
return fig
if __name__ == '__main__':
app.run_server(debug=True)