jupyter notebook inline plots as svg
Asked Answered
M

4

50

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?

Meill answered 14/4, 2016 at 11:52 Comment(0)
P
14

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')
Paez answered 13/6, 2022 at 13:33 Comment(0)
M
73

%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()
Meill answered 14/4, 2016 at 11:52 Comment(6)
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 qualityGardol
Sorry, no I don't know that.Meill
There is a new question being asked about the comment by @markjay , hopefully someone will answer it soon.Arp
@FabianRost InlineBackend.figure_format is deprecated on IPython 2.0.0, use set_matplotlib_formats instead.Inamorato
@Inamorato 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.Shaduf
@Shaduf 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() can set the image formats and other options such as quality at the same time.Inamorato
I
37

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.

Inamorato answered 11/12, 2018 at 7:31 Comment(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.Brass
P
14

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')
Paez answered 13/6, 2022 at 13:33 Comment(0)
W
0

In 2024, the method that is not deprecated is:

%config InlineBackend.figure_formats = ['svg']

Add this to the notebook cell, and all output will be in .svg format.

Westerly answered 14/7, 2024 at 5:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.