Only update some traces in graph

Hi,

I have a graph with a lot of data, then I add three horizontal lines to view a kind of threshold which can be adjusted by changing the input value of an input box.

The graph itself looks like on the attached picture

When I update to the input value to adjust the thresholds(which is just a line between two datasets), the whole graph have to be updated which takes a couple of seconds.

Is it possible to only update the three lines so the rest of the data stays unchanged and doesn’t need to be update?

yes it is possible.
Just give visibility= True,
or Visibility = legendonly

Just look at the plotly documentation https://plot.ly/javascript/reference/#scatter-visible

I don’t think this answers my question…
Because all 4 legends should be visible, I just only want to update 3 of them. The 4’th should somehow be static.

Any updates on this? I have a vertical bar I want to update like OP but I want the data to stay static (most of the time). The performance is horrific since I update the whole figure every time. Can I update the single trace and prevent the performance hit associated with drawing 6000 points?

I have a temporary solution. I found this custom dash component: mydcc from https://github.com/jimmybow/mydcc. He has developed a ChangeTrace dash component to update plotly graph trace (only for graph with one trace).
You can install it with:

pip install mydcc

But, the problem is if you have multiples traces in the graph that component won’t work properly. I changed the code of him a little bit to support multiple traces. But, my version needs all the traces in a list to be updated (traces with new data and traces with old data):

You can try it and see if this temporary solution is suitable for you. The good thing with this is that you don’t need to re-draw the graph again, just all the data in the graph. It will be much faster. In the future a could try a way to set a option to delete a specific trace and then, change its content something like:

fig['data][trace to modify]=new data

2 Likes

Seems somewhat basic, is there really no way to update only some traces in a figure supported officially?
Is there a way to up vote a request for such a feature?

There is the extend data property,

Not exactly what I wanted but it helped, I ended up using State as I found here https://community.plotly.com/t/extend-or-append-data-instead-of-update/8898
Here is a working code example

import dash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output, State
import plotly.graph_objects as go


if __name__ == '__main__':
    app = dash.Dash()
    app.layout = html.Div([dcc.Graph(id='graph'),
                           dcc.Input(id='in', value=7, type='number')])

    @app.callback(
        Output('graph', 'figure'),
        Input('in', 'value'),
        State('graph', 'figure'))
    def update_figure(v, state):
        if state is None:
            fig = go.Figure()
            x = list(range(10))
            y = list(range(5, 25, 2))
            z = [v] * len(x)
            fig.add_trace(go.Scatter(x=x, y=y, mode='lines', name='const'))
            fig.add_trace(go.Scatter(x=x, y=z, mode='lines', name='var'))
            return fig
        offset = [elem['name'] for elem in state['data']].index('var')
        x = state['data'][offset]['x']
        state['data'][offset]['y'] = [v] * len(x)
        return state

    app.run_server()

I think this is a common need so I hope it helps

2 Likes