Include output from %matplotlib notebook backend as SVG in ipynb

Viewed 2663

This answer from a few years ago shows how you can make jupyter notebook create graphs as svg. The solution is to tell the InlineBackend to use svg as output.

import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
plt.plot(...)

This will cause all images to be in svg format inside the notebook as well as in the produced ipynb file; the file will have a line like

"data": {  "image/svg+xml": [  "<?xml  .....

in it.

The problem is now that this does not work if the %matplotlib notebook backend is used. %config InlineBackend does not change anything for the notebook backend, hence the output file contains a PNG image

"data": { "text/html": [  "<img src=\"data:image/png;base64,iVBORw0....

So the question is: How do I get the ipynb file to include a static version of the plot that is created with the %matplotlib notebook backend as SVG image?

There is a small comment by @mark jay from one month ago, who wanted to do exactly what I would like to do now, but there is no answer or hint to that comment.

In my code I have plotted directly from the dataframe:

%matplotlib notebook
import pandas as pd
df = pd.read_sql(sql1, connection)
...
...
df.plot(subplots=True, kind='bar')

This functions perfectly well without importing matplotlib.pyplot but it also can't be coerced to create the graphic as an svg. I suppose if the base case would work, I could modify the plotting code so it did not involve pandas or dataframes.

2 Answers
Related