Search code examples
pythonplotplotlyredrawplotly-express

Plotly: How to update / redraw a plotly express figure with new data?


During debugging or computationally heavy loops, i would like to see how my data processing evolves (for example in a line plot or an image).

In matplotlib the code can redraw / update the figure with plt.cla() and then plt.draw() or plt.pause(0.001), so that i can follow the progress of my computation in real time or while debugging. How do I do that in plotly express (or plotly)?


Solution

  • So i think i essentially figured it out. The trick is to not use go.Figure() to create a figure, but go.FigureWidget() Which is optically the same thing, but behind the scenes it's not.

    documentation

    youtube video demonstration

    Those FigureWidgets are exactly there to be updated as new data comes in. They stay dynamic, and later calls can modify them.

    A FigureWidget can be made from a Figure:

    figure = go.Figure(data=data, layout=layout)
    
    f2 = go.FigureWidget(figure)
    f2                                          #display the figure
    

    This is practical, because it makes it possible to use the simplified plotly express interface to create a Figure and then use this to construct a FigureWidget out of it. Unfortunately plotly express does not seem to have it's own simplified FigureWidget module. So one needs to use the more complicated go.FigureWidget.