Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPython Notebook svg Figures by Default

I just started using ipython, and I'm creating figures such as:

fig, axes = plt.subplots()
xs = range(0,100)
axes.plot(xs, [x*x for x in xs], 'r')

I know that the figures can be rendered as svgs, see here. Unfortunately, the figures are always rendered as a rasterized image. The rasterized images become very ugly when I'm using the notebook's zoom feature. Is there a way to change this behavior, such that figures are displayed as svg by default?

like image 863
Konstantin Weitz Avatar asked Jul 10 '13 22:07

Konstantin Weitz


2 Answers

The magic I was looking for:

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

Alternatively you might still want to show png but save a figure into a file:

plt.savefig(fig_filename, format='svg')
like image 65
Yauhen Yakimovich Avatar answered Feb 07 '23 08:02

Yauhen Yakimovich


You can change the default figure format in the ipython profile configuration files. What I did was create a configuration profile especially for the notebook server, using:

ipython profile create nbserver

At the command line. This creates a whole bunch of files under ~/.ipython/profile_nbserver which have example lines for almost every setting you could want to change (it might be somewhere such as ~/.config/ipython instead depending on your OS, not sure about where it would be under windows). You need to look in the file ipython_notebook_config.py. You should then add the the line:

c.InlineBackend.figure_formats = ['svg']

Note that this only applied to IPython 3.x, and that you can also specify additional formats as per @HarrySchreiner's comment. For IPython 2.x, you should set c.InlineBackEnd.figure_format='svg'. To use this profile you should start the notebook with

ipython notebook --profile=nbserver

If this is too much trouble then don't give a profile name when running create, and modify the default profile instead.

Also, you may want to have the line

c.IPKernelApp.matplotlib = 'inline'

so that each notebook will automatically start with the matplotlib inline backend used.

Originally I also wanted to use the svg backend instead of png to enable zooming etc. However, I found that certain plots, such as pcolor with a large number of points can just kill my browser when using the svg backend. So I find it easier to use png, and just use the xlim and ylim commands to zoom in manually if I need to.

Also, you should definitely tweak the line c.InlineBackend.rc to set more reasonable defaults for the figure size and the fonts used.

edit

Current recommended best practice is not to use pylab, but to explicitly import matplotlib and numpy instead, so I modified my answer to stop encouraging this. See this post for the reasons why:

http://carreau.github.io/posts/10-No-PyLab-Thanks.html

Also, if svg rendering is too slow for particular plot elements (such as pcolor or plot_surface), you can pass the option rasterized=True to these plot commands. This means that those particular parts of the plot will have fast pixel based rendering, but all the other plot elements will be nicely vectorized.

like image 31
DaveP Avatar answered Feb 07 '23 08:02

DaveP