IPython Kernel: Configure Matplotlib as Inline Backend with SVG Format

Bored with unsharp PNG images from your python plots with matplotlib? You can configure your runtime to pick svg by default. Create a code listing like the following

c.InteractiveShellApp.matplotlib = 'inline'
c.InlineBackend.figure_formats = {'svg'}
ipython_kernel_config.py
Python

and mount it at /root/.ipython/profile_default/ipython_kernel_config.py in runtime settings. Note that this will only have effect in a Jupyter runtime. Remix this notebook to inspect the settings. If your runtime is exported as environment, the settings will propagate to notebooks using it.

from matplotlib import pyplot as plt
import numpy as np
x = np.linspace(-4*np.pi, 4*np.pi, 1000)
def f(x): return np.sin(x*x)
plt.plot(x, f(x));
0.6s
Matplotlib Inline svg (Python)
ls -lah /root/.ipython/profile_default
0.5s
Matplotlib Inline svg (Bash in Python)

Test

We test that ipython config is inherited when exporting our runtime as environment for reuse:

from matplotlib import pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 1000)
plt.plot(x, np.sin(x));
0.8s
Test I (Python)
Matplotlib Inline svg

Without Config

no plot is displayed without invoking the matplotlib magic

import matplotlib.pyplot as plt
plt.plot([1, 3, 2])
1.3s
Test II (Python)
[<matplotlib.lines.Line2D at 0x7f6583029350>]

default format is PNG

%matplotlib inline
plt.plot([1, 3, 2])
0.4s
Test II (Python)
Runtimes (3)