4

I'm creating a plotly chart in jupyter notebook. Because I'm testing some algorithm I want to add data after the initial fig2.show(). But when I update the data and call fig2.show again a new chart is being rendered. How can I update the chart instead of creating a new chart?

This should be an easy task - but it's way to hard to find for me in the documentation.

import plotly.offline as pyo
import plotly.graph_objs as go
import numpy as np
# Set notebook mode to work in offline
pyo.init_notebook_mode()
fig2 = go.Figure(data=go.Scatter(x=moreX2, y=moreY2))
fig2.show()

Then I'm using update:

moreX2.append(2)
moreY2.append(5)
fig2.data[0].update({"x": moreX2, "y": moreY2})

The chart isn't being rerendered and I tried to call fig.show() again, which just creates a new chart.

Matthias Herrmann
  • 2,650
  • 5
  • 32
  • 66
  • When working with plotly in Jupyter, AFAIK you have to run fig.show() (or even just 'fig') to update the chart. Why not have two Jupyter cells: one with the calculation, and another one with the chart, and re-run the one with the chart when you have new data? – Roy2012 Jun 20 '20 at 11:57

1 Answers1

0

This answer worked for me, and seems like the ideal answer to this question as well. I've voted to mark this as a duplicate.

To summarize: by wrapping the Figure in a FigureWidget, we can now do exactly what the OP wanted. Although on Colab I did have to give some extra permission by calling the following:

from google.colab import output
output.enable_custom_widget_manager()
Neil Traft
  • 18,367
  • 15
  • 63
  • 70