Black Lives Matter. Please consider donating to Black Girls Code today.

Turn off automatic graph update while updating traces

Currently I am working on an interactive plot using Jupyter notebook and ipywidgets, but I would like to be able to turn automatic updates of the graph on and off.

First I create a figure with a number of sliders to set the inputs, which are used to update about 15 different lines in 4 subplots. However, when I run the self.update_scatter() function, every time the whole graph is updated, which makes the update both a slow and ugly process.
What I want to do is to update all the lines in the background and then update the whole figure at once, but I cannot find a setting for this. Is it possible to turn off the automatic updates and manually update after I changed the graphs?

My update function looks like this:

def update_y_axis_old(change):

    # Read data 
    [plot_data, axis_label, axis_val] = load_data()

    # Some option to turn of automatic updates.....

    # Update all traces.
    for pol, col, row_no, in zip(['HH', 'HV'], ['blue', 'red'], [1, 2]):
        # Plot amplitude plot

        plot.update_traces(y=plot_data['percentiles_1_' + pol], 
                           selector=dict(name= str(percentiles[1]) + ' percentile ' + pol), row=1, col=1)
        plot.update_traces(y=plot_data['median_' + pol], selector=dict(name='amplitude ' + pol), row=1, col=1)
        plot.update_traces(y=plot_data['percentiles_2_' + pol], 
                           selector=dict(name= str(percentiles[0]) + ' percentile ' + pol), row=1, col=1)
        # Plot coherence plots
        plot.update_traces(y=plot_data['coherence_baseline_' + pol], 
                           selector=dict(col=2, row=row_no, name='baseline'))
        plot.update_traces(y=plot_data['coherence_vol_' + pol], selector=dict(name='volume'), col=2, row=row_no)
        plot.update_traces(y=plot_data['coherence_snr_' + pol], selector=dict(name='SNR'), col=2, row=row_no)
        plot.update_traces(y=plot_data['coh_percentiles_1_' + pol], 
                           selector=dict(name= str(percentiles[0]) + ' percentile'), row=row_no, col=2)
        plot.update_traces(y=plot_data['coherence_' + pol], selector=dict(name='total'), col=2, row=row_no)
        plot.update_traces(y=plot_data['coh_percentiles_2_' + pol], 
                           selector=dict(name= str(percentiles[1]) + ' percentile'), row=row_no, col=2)

        # Plot std plot
        for look in looks:
            plot.update_traces(y=plot_data['std_meters_' + pol + '_' + str(look)], 
                                 selector=dict(name=pol + ' ' + str(look) + ' looks'), row=2, col=1)

    # Finally do the update to the full plot.

I also tried some other ways to update the lines, but I get the same problem. The issue is here that plotly is updating the lines in the graph one by one, while I want to do the update all at once.

Hi @gmulder,

This issue is actually not really related to plotly but to ipywidgets. It is quite hard to give you a direct solution based on your code but on suggestion is to use a checkbox widget to turn off/on the update.

Here a very generic example:

import plotly.graph_objects as go
import ipywidgets as widgets
import numpy as np

def update_fig(freq, phase, update):
    if update:
        fig.data[0].y = np.sin(2*np.pi*freq*x + phase)
        
update_checkbox = widgets.Checkbox(value = True, description='update fig')
freq_slider = widgets.FloatSlider(min=0.1, max=1)
phase_slider = widgets.FloatSlider(min=np.pi, max=2*np.pi)
interact_objs = widgets.interactive(update_fig, freq=freq_slider, phase=phase_slider, update=update_checkbox)       
x = np.linspace(0,2*np.pi,100)
fig = go.FigureWidget(go.Scatter(x=x))
display(interact_objs, fig)

hope it helps

Alex-