Enable output from% matplotlib backend as SVG in ipynb

This answer from a few years ago shows how you can make jupyter notebook create graphs like svg. The solution is to tell 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 laptop, as well as in the created ipynb file; the file will have a line like

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

in him.

The problem is that this does not work if the backend is %matplotlib notebook . %config InlineBackend doesn’t change anything for the backend laptop, so 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 an ipynb file to include a static version of a graph created using %matplotlib notebook backend as an SVG image?

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

In my code, I built directly from the data frame:

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

This works fine without importing matplotlib.pyplot , but it also cannot be forced to create a graphic like svg. I believe that if the base case worked, I could change the build code, so it did not include pandas or dataframes.

+8
python matplotlib pandas jupyter-notebook
source share
2 answers

From what I understand from reading about matplotlib backends, nbagg, which is called using %matplotlib notebook , uses Agg (Anti-Grain Geometry) rendering, which is not able to display vector graphics. Unfortunately, this is the only way to use the built-in interactive backend for Jupyter.

Docs Link https://matplotlib.org/faq/usage_faq.html#what-is-interactive-mode
Related Answers How to make matplotlibs nbagg backend generate SVG?

If you don't need interactivity, just keep using

 import pandas as pd from IPython.display import SVG, display from numpy import ndarray def svg_add(chart, size=(4,4), dpi=100): """Takes a chart, optional tuple of ints for size, int for dpi default is 4 by 4 inches with 100 dpi""" if type(chart) == ndarray: fig = chart[0].get_figure() fig.set_size_inches(size) fig.savefig("mybar.svg", dpi=dpi) display(SVG(filename='mybar.svg')) else: fig = chart.get_figure() fig.set_size_inches(size) fig.savefig("mybar.svg", dpi=dpi) display(SVG(filename='mybar.svg')) 

then

 df = pd.DataFrame([[2,5]],columns=['a','b']) bar_chart = df.plot(subplots=False, kind='bar') svg_add(chart=bar_chart,size=(3,3),dpi=100) #or #svg_add(bar_chart,(3,3),100) 
+1
source share

Since, apparently, even after a period of generosity, no one was able to provide a solution, the following could be a workaround.

  • Create a laptop using %matplotlib notebook . Once you are satisfied with the result, save it.
  • Use a copy of it and replace %matplotlib notebook with

     %matplotlib inline %config InlineBackend.figure_format = 'svg' 

    Reboot the laptop. Save the result.

  • Open the resulting ipynb file in a text editor and replace the previous two lines again with %matplotlib notebook .

The end result will be ipynb with svg images. But after its opening and launch, he will use the backend to create drawings.

+1
source share

All Articles