Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting jupyter notebook to pdf with offline plotly graph; missing graphs

I am trying to create pdf export of my lesson plans and I use plotly offline for the graphs. In a MWE below, the plot will display in the Jupyter Notebook but will not show up when I export to pdf. I export using File-->Download as-->PDF via Latex (.pdf).

I'd like to make a pdf instead of using html. I understand it might take an extra step to convert an html export to pdf, but I was just wondering if there was a more direct route (a code modification?) that would allow me to export directly through File-->Download as-->PDF via Latex (.pdf)

from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
init_notebook_mode(connected=True)
import plotly.graph_objs as go

data = [go.Scatter(
    x=[1, 2, 3],
    y=[3, 2, 1])
]
iplot(data)
like image 878
jtorca Avatar asked Aug 18 '17 17:08

jtorca


People also ask

How do I export a Jupyter Notebook to PDF?

The Jupyter Notebook has an option to export the notebook to many formats. It can be accessed by clicking File -> Download as -> PDF via LaTeX (or PDF via HTML - not visible in the screenshot).

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.

Why Plotly does not show in Jupyter lab?

JupyterLab Problems In order to use plotly in JupyterLab, you must have the jupyterlab-plotly extension installed as detailed in the Getting Started guide. When you install plotly , this extension is automatically made available to any JupyterLab 3. x installation in the same Python environment.

Why is my Plotly not working?

Why is my plotly not working? If you are encountering problems using plotly with Dash please first ensure that you have upgraded dash to the latest version, which will automatically upgrade dash-core-components to the latest version, ensuring that Dash is using an up-to-date version of the Plotly.


1 Answers

You need to specify the appropriate Default Renderer (or Renderers, if you want to visualize it in the Notebook and also when exporting to PDF using the File-->Download as-->PDF via Latex (.pdf) option you mentioned).

I have been struggling with this myself for some hours too, but the setup that ended up working for me is the following:

import plotly.io as pio
pio.renderers.default = "notebook+pdf"  # Renderer for Notebook and HTML exports + Renderer for PDF exports
# init_notebook_mode(connected=True)  # Do not include this line because it may not work

Note that you can concatenate as many Renderers as you want using the + symbol, and Plotly will magically know when to use each of them.

like image 50
dsesto Avatar answered Nov 13 '22 16:11

dsesto