46

By default jupyter notebook inline plots are displayed as png, e.g.:

import matplotlib.pyplot as plt
%matplotlib inline
plt.plot()

How can you configure jupyter notebooks to display matplotlib inline plots as svg?

Fabian Rost
  • 2,284
  • 2
  • 15
  • 27

3 Answers3

71

%config InlineBackend.figure_formats = ['svg'] does the trick. A minimal example is:

%config InlineBackend.figure_formats = ['svg']

import matplotlib.pyplot as plt
%matplotlib inline
plt.plot()
Fabian Rost
  • 2,284
  • 2
  • 15
  • 27
  • 2
    do you know if there's a similar fix when using `%matplotlib notebook`? I can't seem to find a way to make the figure display as svg quality – mark jay Jul 09 '17 at 21:08
  • Sorry, no I don't know that. – Fabian Rost Jul 10 '17 at 21:41
  • 1
    There is a [new question](https://stackoverflow.com/questions/45466947/include-output-from-matplotlib-notebook-backend-as-svg-in-ipynb) being asked about the comment by @markjay , hopefully someone will answer it soon. – ImportanceOfBeingErnest Aug 04 '17 at 10:28
  • 1
    @FabianRost `InlineBackend.figure_format` is [deprecated](https://ipython.readthedocs.io/en/stable/whatsnew/version2.0.html?highlight=figure_formats#selecting-matplotlib-figure-formats) on IPython 2.0.0, use [`set_matplotlib_formats`](https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html#IPython.display.set_matplotlib_formats) instead. – nekketsuuu Dec 11 '18 at 07:03
  • 1
    @nekketsuuu `InlineBackend.figure_format` is deprecated in favor of `InlineBackend.figure_formats` (note the extra 's' at the end), which supports specifying multiple formats. Other than this, the usage through `%config` is not deprecated. – Zoltan Mar 08 '19 at 13:37
  • 1
    @Zoltan Thanks for clarifying! It's written in the changelog I linked so I've known that, but I prefer to use less magics. Also, [`set_matplotlib_formats()`](https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html#IPython.display.set_matplotlib_formats) can set the image formats and other options such as quality at the same time. – nekketsuuu Mar 11 '19 at 01:11
36

Use set_matplotlib_formats('svg').

import matplotlib.pyplot as plt
from IPython.display import set_matplotlib_formats
%matplotlib inline
set_matplotlib_formats('svg')
plt.plot()

This is also documented in a document of %matplotlib magic.

Note: InlineBackend.figure_format is deprecated.

nekketsuuu
  • 1,641
  • 1
  • 21
  • 26
  • 1
    It seems like this form is now deprecated too. I get: `set_matplotlib_formats` is deprecated since IPython 7.23, directly use `matplotlib_inline.backend_inline.set_matplotlib_formats()` after removing the cwd from sys.path. – James Brusey Aug 20 '21 at 14:33
  • 4
    The correct solution is now the one above: `%config InlineBackend.figure_formats = ['svg']` – ragazzojp Nov 19 '21 at 10:56
8

set_matplotlib_formats() is deprecated since May 2021.

DeprecationWarning: set_matplotlib_formats is deprecated since IPython 7.23, directly use matplotlib_inline.backend_inline.set_matplotlib_formats() set_matplotlib_formats('svg')

Current Working method:

%matplotlib inline
import matplotlib_inline
matplotlib_inline.backend_inline.set_matplotlib_formats('svg')
Shriraj Hegde
  • 829
  • 5
  • 18