Skip to content

Commit 49472aa

Browse files
committed
Add set_matplotlib_formats and set_matplotlib_close
1 parent 09f4eb0 commit 49472aa

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

matplotlib_inline/backend_inline.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from matplotlib import colors
1313
from matplotlib._pylab_helpers import Gcf
1414

15+
from IPython.core.interactiveshell import InteractiveShell
1516
from IPython.core.getipython import get_ipython
1617
from IPython.core.pylabtools import select_figure_formats
1718
from IPython.display import display
@@ -170,7 +171,7 @@ def configure_inline_support(shell, backend):
170171
if cfg not in shell.configurables:
171172
shell.configurables.append(cfg)
172173

173-
if backend == 'module://ipykernel.pylab.backend_inline':
174+
if backend == 'module://matplotlib_inline.backend_inline':
174175
shell.events.register('post_execute', flush_figures)
175176

176177
# Save rcParams that will be overwrittern
@@ -250,3 +251,56 @@ def _is_transparent(color):
250251
"""Determine transparency from alpha."""
251252
rgba = colors.to_rgba(color)
252253
return rgba[3] < .5
254+
255+
256+
def set_matplotlib_formats(*formats, **kwargs):
257+
"""Select figure formats for the inline backend. Optionally pass quality for JPEG.
258+
259+
For example, this enables PNG and JPEG output with a JPEG quality of 90%::
260+
261+
In [1]: set_matplotlib_formats('png', 'jpeg', quality=90)
262+
263+
To set this in your config files use the following::
264+
265+
c.InlineBackend.figure_formats = {'png', 'jpeg'}
266+
c.InlineBackend.print_figure_kwargs.update({'quality' : 90})
267+
268+
Parameters
269+
----------
270+
*formats : strs
271+
One or more figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
272+
**kwargs
273+
Keyword args will be relayed to ``figure.canvas.print_figure``.
274+
"""
275+
# build kwargs, starting with InlineBackend config
276+
cfg = InlineBackend.instance()
277+
kw = {}
278+
kw.update(cfg.print_figure_kwargs)
279+
kw.update(**kwargs)
280+
shell = InteractiveShell.instance()
281+
select_figure_formats(shell, formats, **kw)
282+
283+
284+
def set_matplotlib_close(close=True):
285+
"""Set whether the inline backend closes all figures automatically or not.
286+
287+
By default, the inline backend used in the IPython Notebook will close all
288+
matplotlib figures automatically after each cell is run. This means that
289+
plots in different cells won't interfere. Sometimes, you may want to make
290+
a plot in one cell and then refine it in later cells. This can be accomplished
291+
by::
292+
293+
In [1]: set_matplotlib_close(False)
294+
295+
To set this in your config files use the following::
296+
297+
c.InlineBackend.close_figures = False
298+
299+
Parameters
300+
----------
301+
close : bool
302+
Should all matplotlib figures be automatically closed after each cell is
303+
run?
304+
"""
305+
cfg = InlineBackend.instance()
306+
cfg.close_figures = close

0 commit comments

Comments
 (0)