I am using Plotly offline on Jupyter.
I am plotting curves:
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
from plotly.graph_objs import *
import datetime as dt
list_date = [dt.datetime(2016,1,1).date(), dt.datetime(2016,1,2).date(), dt.datetime(2016,1,3).date(), dt.datetime(2016,1,4).date()]
data = []
for i in range(3) :
list = [i/2+1, i/2+2, i/2+3, i/2+4]
data.append(Scatter(x=list_date, y=list, name='y'+str(i)))
figure = Figure(data=data)
iplot(figure)
And I get a very nice graph! In the latter case, the user wants to add a bar graph on it (in addition to the two lines already there).
list_bar = [0.5, 1.5, 2.5, 3.5]
data = [Bar(x=list_date, y=list_bar, name='bar')]
figure.update(data=data)
iplot(figure)
But I have only the bar chart, not the previous 2 lines. How to have offline the equivalent of the online function fileopt='append'
?
py.plot(data, filename='append plot', fileopt='append')
In the latest plotly version 3, a FigureWidget has been added specifically to handle your problem of wanting to update an existing offline figure.
For me, running pip install plotly --upgrade
got me the latest version within my Anaconda environment.
I've modified your example code below to use the new FigureWidget, and left in your old code that needed changing with comments. The new FigureWidget is meant to be compatible with the ordinary Figure that you were using.
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
from plotly.graph_objs import *
import datetime as dt
# list_date = [dt.datetime(2016,1,1).date(), dt.datetime(2016,1,2).date(), dt.datetime(2016,1,3).date(), dt.datetime(2016,1,4).date()]
list_date = [dt.datetime(2016,1,1), dt.datetime(2016,1,2), dt.datetime(2016,1,3), dt.datetime(2016,1,4)]
data = []
for i in range(3) :
list = [i/2+1, i/2+2, i/2+3, i/2+4]
data.append(Scatter(x=list_date, y=list, name='y'+str(i)))
# figure = Figure(data=data)
# iplot(figure)
figure = FigureWidget(data=data)
figure
I've commented out the portions that were changed so you can see them for reference.
One other thing to note, due to a problem within ipykernel.json_util, the json_clean function that serializes Plotly JSON objects to show on your Jupyter screen doesn't know what to do with a datetime.date object -- only datetime objects. If you don't remove the .date you will get an exception and no graph. I'm guessing this would happen for datetime.time objects as well because it seems it is also unhandled in the current ipykernel code.
When you're ready to run your updated code, you simple create your data and use the add_trace
function:
list_bar = [0.5, 1.5, 2.5, 3.5]
figure.add_trace(Bar(x=list_date, y=list_bar, name='bar'))
And your plot automatically updates in the previous cell with the added trace.
Lastly, there's a good guide about the new FigureWidget for those interested.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With