|
12 | 12 | from matplotlib import colors
|
13 | 13 | from matplotlib._pylab_helpers import Gcf
|
14 | 14 |
|
| 15 | +from IPython.core.interactiveshell import InteractiveShell |
15 | 16 | from IPython.core.getipython import get_ipython
|
16 | 17 | from IPython.core.pylabtools import select_figure_formats
|
17 | 18 | from IPython.display import display
|
@@ -170,7 +171,7 @@ def configure_inline_support(shell, backend):
|
170 | 171 | if cfg not in shell.configurables:
|
171 | 172 | shell.configurables.append(cfg)
|
172 | 173 |
|
173 |
| - if backend == 'module://ipykernel.pylab.backend_inline': |
| 174 | + if backend == 'module://matplotlib_inline.backend_inline': |
174 | 175 | shell.events.register('post_execute', flush_figures)
|
175 | 176 |
|
176 | 177 | # Save rcParams that will be overwrittern
|
@@ -250,3 +251,56 @@ def _is_transparent(color):
|
250 | 251 | """Determine transparency from alpha."""
|
251 | 252 | rgba = colors.to_rgba(color)
|
252 | 253 | 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