Vector graphics export using nbconvert to LaTeX

Dear all,

I am using offline in Jupyter Notebooks in Python. See the example code below. When exporting the notebook to LaTeX using nbconvert, the interactive PlotLy figure gets converted to PNG. Is there a way to make it export to vector graphics instead? (SVG, EPS or PDF). Is the PNG conversion done by nbconvert itself or by plotly or orca?

I know that I could either:

  • use the write_image method to save the figure to SVG, EPS or PDF. This is what I would use for publications, but for regular documents, I would like to avoid having to save figures manually before generating the LaTeX files
  • use to_image(fig, format=โ€˜svgโ€™) and then display the SVG using the IPython.display.SVG method. However, nbconvert fails at the moment because of SVG, the pull request is still pending:

The code I use:

import plotly.offline as py
import plotly.graph_objs as go

trace = go.Scatter(
    x = df.loc['A']['X'],
    y = df.loc['A']['Y'],
data = [trace]
layout = go.Layout(
    title = 'A',
    xaxis = dict(
        title = 'X'
    yaxis = dict(
        title = 'Y'
fig = dict(data=data, layout=layout)

I use the following command in the terminal to convert the notebook to LaTeX:

jupyter nbconvert --template=myTemplate.tplx --to=latex myNotebook.ipynb

My setup:

  • Ubuntu 18.04 x64
  • conda environment containing:
    • plotly 3.7.0 from plotly conda channel
    • plotly-orca 1.2.1 from plotly conda channel
    • notebook 5.7.6 from conda-forge channel
    • jupyterlab 0.35.4 from conda-forge channel
    • nbconvert 5.4.1 from conda-forge channel

Thank you,
Best regards

Hi @GorgiAstro,

Before running nbconvert, are you creating your notebooks in JupyterLab? If so, the png image that nbconvert is finding is actually generated by the @jupyterlab/plotly-extension extension. This extension automatically saves a png version of the figure back to the notebook on render. See:

Your usecase isnโ€™t directly supported yet, but itโ€™s one of the motivations for the proposal (See that I wroteup a few days ago. Under this proposal you would put a line at the top of the notebook like.

import as pio
pio.renderers.default = 'jupyterlab+svg`

Then every time a figure is displayed it would be stored to the notebook using both the plotly mimetype (the interactive type handled by @jupyterlab/plotly-extension) and svg. In JupyterLab you would get the interactive display, but nbconvert should pick up the svg format instead.

Feel free to chime in on with your use-case, and let me know if youโ€™re interested in helping out with testing this workflow once we have some development builds ready.


Hi @jmmease,

Thank you for your answer. I am working in JupyterLab indeed.

I like your proposal for the, the approach is elegant. I am definitely interested in helping out with testing, I subscribed to the Github ticket.


I have a PR ready to play with now. See and let me know if you give it a try!


Great, thanks a lot! I will give it a try tonight or tomorrow.


1 Like