5
votes

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)
4

4 Answers

2
votes

I think because plotly graphs are svg objects and generated by javascript, I dont have export to PDF working in my jupyter notebook, so I was unable to check and confirm my answer.

Plotly offline does not have show as image, you can use plotly online to do this, its free to generate graphs,

You need to create an online account, also you need to paste the username and API key from plotly website (API key can be found in settings).

Note: please check in plotly if the plots are shared in public or private, I am not responsible for your plots becoming public.

Anyway this will give you an image output of the graph, and you can export it to PDF

Code:

import plotly
import plotly.graph_objs as go

plotly.plotly.sign_in('<<username goes here>>', '<<api key goes here>>')
trace = go.Bar(x=[2, 4, 6], y= [10, 12, 15])
data = [trace]
layout = go.Layout(title='A Simple Plot', width=800, height=640)
fig = go.Figure(data=data, layout=layout)

plotly.plotly.image.save_as(fig, filename='a-simple-plot.png')

from IPython.display import Image
Image('a-simple-plot.png')
1
votes

A bit easier solution is to use image_bytes = fig.to_image(format='png') and than display using Image(image_bytes), but firstly you should install orca, and
pip3 install psutil requests
example:

fig = go.Figure()
""" here goes some code to draw """

image_bytes = fig.to_image(format='png', , width=1200, height=700, scale=1) # you can use other formats as well (like 'svg','jpeg','pdf')

#instead of using fig.show()
from IPython.display import Image
Image(img_bytes)

You can read more here

Than you can convert it with File -> Download as -> PDF or using terminal jupyter nbconvert --to pdf <notebook_name>.ipynb

1
votes

I don't have a solution for the exact original problem above. However, if you wish to consider the .html format, here is a nice solution that worked for me. After all, my goal was just to share the notebook with people who didn't have jupyter installed.

The conversion to .html is performed with plotlyhtmlexporter. Here is a piece of code you can copy-paste:

  pip install plotlyhtmlexporter
  jupyter nbconvert --to plotlyhtml mynotebook.ipynb

You might then want to try and save (print) the .html from your browser as a .pdf, but I think it would be equivalent to just printing the original notebook to .pdf. As I say, in my case, having a jupyter-independent .html file was enough.

1
votes

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.