Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions IPython/core/magics/pylab.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Our own packages
from IPython.config.application import Application
from IPython.core import magic_arguments
from IPython.core.magic import Magics, magics_class, line_magic
from IPython.core.magic import Magics, magics_class, line_magic, line_cell_magic
from IPython.testing.skipdoctest import skip_doctest
from IPython.utils.warn import warn
from IPython.core.pylabtools import backends
Expand All @@ -38,18 +38,24 @@
@magics_class
class PylabMagics(Magics):
"""Magics related to matplotlib's pylab support"""

@skip_doctest
@line_magic
@line_cell_magic
@magic_arguments.magic_arguments()
@magic_gui_arg
def matplotlib(self, line=''):
def matplotlib(self, line='', cell=None):
"""Set up matplotlib to work interactively.

This function lets you activate matplotlib interactive support
at any point during an IPython session. It does not import anything
into the interactive namespace.


This function can be used both as a line and cell magic.

As a line magic this function lets you activate matplotlib interactive
support at any point during an IPython session. It does not import
anything into the interactive namespace.

As a cell magic, the matplotlib backend is reverted back to its
previous value after the execution of the cell. If matplotlib
interactive support was not active, the choosen backend remains active.

If you are using the inline matplotlib backend in the IPython Notebook
you can set which figure formats are enabled using the following::

Expand All @@ -75,10 +81,29 @@ def matplotlib(self, line=''):
But you can explicitly request a different GUI backend::

In [3]: %matplotlib qt

To enable the inline backend only for one cell and return to the
previous backend (qt in this case) afterwards::

In [4]: %%matplotlib inline
"""
if cell is not None:
if self.shell.pylab_gui_select is None:
# Check if the inline backend is active
import matplotlib
if matplotlib.rcParams["backend"] == backends["inline"]:
previous_gui = "inline"
else:
previous_gui = None
else:
previous_gui = self.shell.pylab_gui_select
args = magic_arguments.parse_argstring(self.matplotlib, line)
gui, backend = self.shell.enable_matplotlib(args.gui)
self._show_matplotlib_backend(args.gui, backend)
if cell is not None:
self.shell.run_cell(cell)
if previous_gui is not None:
self.shell.enable_matplotlib(previous_gui)

@skip_doctest
@line_magic
Expand Down
4 changes: 4 additions & 0 deletions docs/source/whatsnew/pr/matplotlib_cell_magic.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* The :magic:`matplotlib` magic can now act as a cell magic, in which case, the
matplotlib backend is reverted back to its previous value after the execution
of the cell. If matplotlib interactive support was not active, the choosen
backend remains active.