Thursday, 15 July 2021

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 sqlite3
import plotly
from app import app
import plotly.graph_objs as go
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Output, Input



layout = html.Div([html.Div([
                       dcc.Graph(id='live-graph'),
                       dcc.Interval(
                           id='graph-update',
                           interval=1 * 1000
                       ),
                   ])
])

latest_sno = 0


@app.callback(Output('live-graph', 'figure'),
              [Input('graph-update', 'n_intervals')])
def update_graph_scatter(input_data):
    X = list()
    Y = list()
    connection = sqlite3.connect(r'C:\Users\name\Desktop\database\databse.db',)
    connection.execute('pragma journal_mode=wal')
    data = connection.cursor()
    data.execute('Select SNO FROM cvt')
    for i in data.fetchall():
        X.append(i.__getitem__(0))
    data.execute('Select val FROM cvt')
    for i in data.fetchall():
        Y.append(i.__getitem__(0))

    data = plotly.graph_objs.Scatter(
        x=X,
        y=Y,
        name='cell1',
        mode='lines'
    )
    if (len(X)) > 1000:
        x_l = max(X) - 1000
    else:
        x_l = 0
    return {'data': [data,data1], 'layout': go.Layout(title="plot1",
                                                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",
                                                )}



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

No comments:

Post a Comment