Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Persistent plotly plots in jupyter notebooks

My jupyter notebooks that have plotly plots do not retain the plots between sessions.

This is running on a Singularity container based on the official jupyter/datascience-notebook docker image with plotly pip installed on top.

I am using the new renderer framework with the notebook renderer. My notebooks are trusted. My plots show up during the session without issues. They persist across refreshes and reloads of the same notebook, even if I restart the kernel. They disappear either when I restart the jupyter server or sometimes when I reboot the client machine and come back with a new browser session. The output cells persist with the correct dimensions, but they are blank. I can see that a whole bunch of js is embedded in the notebook but it does not render in the browser. At this point, even if I nbconvert to html, they still do not show up. Tried with Chromium and Firefox.

import plotly.graph_objects as go
import plotly.io as pio
import plotly.express as px
pio.renderers.default='notebook'

then later I plot a bunch of things like:

go.Figure(go.Scattergl(x = var1, y= var2, mode='markers', marker_size=1))

and

go.Figure(go.Histogram2dContour(x = var1, y= var2))

My understanding is that I am set up to retain these figures in offline (non-running) notebooks; the js generated for the plots and the entirety of plotly.js library appears to be embedded in each notebook adding up to 10s of MBs, but they are not rendered. Due to this issue I end up having to re-run (sometimes expensive) notebooks when all I need is to take a look at a previous plot.

As a recent matplotlib/seaborn convert I absolutely love the interactivity but this is quickly becoming a showstopper at this point. I feel like I'm missing something. Any advice is appreciated.

like image 989
littleblackfish Avatar asked Sep 12 '19 20:09

littleblackfish


People also ask

Can plotly be used in Jupyter notebook?

You can publish Jupyter Notebooks on Plotly.

How do you show plot in Jupyter notebook?

Usually, displaying plots involves using the show() function from PyPlot. With Jupyter notebooks, this isn't necessary as the plots are displayed after running the cells containing the code that generates them. These plots are by default, displayed inline, which means, they're displayed in the notebook itself.

Can plotly be used offline?

Plotly's open-source graphing libraries are free to use, work offline and don't require any account registration. Plotly also has commercial offerings, such as Dash Enterprise and Chart Studio Enterprise.


1 Answers

For me, the following solution worked :

  • in the environment you're using : install orca : conda install -c plotly plotly-orca
  • at the beginning of your notebook : override default renderer as 'notebook' :
import plotly.io as pio
pio.renderers.default='notebook'

plotly graphs were persistent between sessions (ie : after restart of kernels) with these modifications.

like image 55
user16461867 Avatar answered Oct 07 '22 21:10

user16461867