rohith santosh
rohith santosh

Reputation: 40

How do I increase the efficiency (speed) of dash-plotly line graph in python

I am using dash plotly in python. I am plotting real-time data which is being logged into an SQLite database, currently, I am plotting a single value vs timeline graph. I am planning to add 20 more graphs to this but currently, as time increases the plot gets slower I think it's due to the replotting of the entire plot again. so could anyone please let me know if there an efficient way to do this? I am new to dash-plotly so any help would be a great help to me.

Thank you.

import random
import dash
import plotly
import plotly.graph_objs as go
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Output, Input

X = list()
X.append(0)
Y = list()
Y.append(0)
app = dash.Dash(__name__, suppress_callback_exceptions=True,)
app.layout = html.Div([html.Div([
    dcc.Graph(id='live-graph'),
    dcc.Interval(
        id='graph-update',
        interval=0.05 * 1000
    ),
])
])

latest_sno = 0


@app.callback(Output('live-graph', 'figure'),
              [Input('graph-update', 'n_intervals')])
def update_graph_scatter(input_data):
    X.append(X[-1]+1)
    Y.append(0+random.randint(-2,2))
    data = plotly.graph_objs.Scatter(
        x=X,
        y=Y,
        name='lines',
        mode='lines'
    )
    if (len(X)) > 1000:
        x_l = max(X) - 1000
    else:
        x_l = 0
    return {'data': [data], 'layout': go.Layout(title="BMS_01_CellVolt_AVG01",
                                                xaxis=dict(range=[x_l, max(X) + 1]),
                                                yaxis=dict(range=[min(Y) - 0.15, max(Y) + 1]),
                                                yaxis_title="Voltage in (V)",
                                                xaxis_title="TIME",
                                                )}
if __name__ == '__main__':
    app.run_server(debug=True,port = 5050)

Upvotes: 5

Views: 2953

Answers (1)

rohith santosh
rohith santosh

Reputation: 40

ok After struggling for so many days I found a solution for my question. you can achieve it using extendable graphs follow this link to find the sample code https://community.plotly.com/t/extend-or-append-data-instead-of-update/8898/25

Upvotes: 1

Related Questions