[Python] Fast plotly updating inside Jupyter

Discussão em 'Python' iniciado por Stack, Agosto 25, 2025.

  1. Stack

    Stack Membro Participativo

    I am running a Jupyter notebook inside VSCode. I want to use plotly (or anything else, but I am trying this as a potential solution) to dynamically plot data as it is generated.

    I tried to generate a minimum working example of what I want. In the following, I generate some fake data and then update the plotly figure to simulate that data being generated in real time.

    In the code below, the first cell should be run and the figure allowed to draw before the second cell is run (I don't know why these steps have to be split, but the figure does not appear if all the code is in one cell). The second cell does the thing I am most interested in.

    What I see is that the time taken to update is extremely slow - around 2-3 seconds for each 'tval' loop iteration. If I reduce 'ntraces' to 1, then it becomes much faster, suggesting that part of the bottleneck is having to iterate through each of the traces individually to update the data. It seems that if there was a way to update a 2D array, rather than a number of separate 1D arrays, it might be quicker.

    If there are ideas of how to speed this up, or an alternative approach which is much faster, I would be very grateful.

    import plotly.graph_objects as go
    import numpy as np
    import random

    T = 20
    tvals = np.arange(1, 101, 2)
    noise_amp = 0.25
    tau = 60
    ntraces = 32
    traces = np.arange(0, ntraces, 1)
    tarray = np.arange(1, 51, 2)
    pvalsArray = [np.array([np.sin(2*np.pi*tval/T)*np.exp(-tval/tau)+random.random()*noise_amp for tval in tarray]) for _ in traces]
    fig = go.FigureWidget()

    for i, ion in enumerate(traces):
    fig.add_trace(go.Scatter(x=tarray, y=pvalsArray[0:1], mode='lines+markers', name='lines'))
    fig.show()


    for j, tval in enumerate(tarray):
    for i in range(len(traces)):
    fig.data.x = tarray[:j]
    fig.data.y = pvalsArray[0:j]

    Continue reading...

Compartilhe esta Página