Updating and showing a plot in a loop


I would like to monitor some results from calculations generated in a loop while the calculations are running. I have prepared this example to show what I would like to do. My questions are at the end.

import plotly.graph_objects as go

# some data I want to look at while I am doing some calculations
trace1 = go.Scatter(x=[1, 2], y=[2, -1])
fig1 = go.Figure(data=trace1)
fig1.show()  # This shows the plot in my browser

for i in range(5):
    # here I do the calculations. I pretend the results are stored in "i"
    # ...

    # Here I update the y data with my results in "i" of the go.Scatter
    # trace that I use to look at my data
    trace1.y = [2, i]

    # updating the axis limits according to the updated data:
        xaxis=dict(range=[min(trace1.x), max(trace1.x)]),
        yaxis=dict(range=[min(trace1.y), max(trace1.y)]))

    # this point in the code is where I have questions.

Question 1:
How do I update the plot fig1 at such that it just refreshes the plot with the new values in the existing trace β€œtrace1” in the browser tab where it is already open?

Question 2:
what if I want to add traces in the loop and then refresh the existing plot like in question 1 with the new trace added?

1 Like