Module:core.magics.pylab

Implementation of magic functions for matplotlib/pylab support.

1 Class

classIPython.core.magics.pylab.PylabMagics(**kwargs:Any)

Bases:Magics

Magics related to matplotlib’s pylab support

matplotlib(line='')
%matplotlib[-l][gui]

Set up matplotlib to work interactively.

This function lets you activate matplotlib interactive supportat any point during an IPython session. It does not import anythinginto the interactive namespace.

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

In[1]:frommatplotlib_inline.backend_inlineimportset_matplotlib_formatsIn[2]:set_matplotlib_formats('pdf','svg')

The default for inline figures setsbbox_inches to ‘tight’. This cancause discrepancies between the displayed image and the identicalimage created usingsavefig. This behavior can be disabled using the%config magic:

In[3]:%configInlineBackend.print_figure_kwargs={'bbox_inches':None}

In addition, see the docstrings ofmatplotlib_inline.backend_inline.set_matplotlib_formats andmatplotlib_inline.backend_inline.set_matplotlib_close for more information onchanging additional behaviors of the inline backend.

Examples

To enable the inline backend for usage with the IPython Notebook:

In[1]:%matplotlibinline

In this case, where the matplotlib default is TkAgg:

In[2]:%matplotlibUsingmatplotlibbackend:TkAgg

But you can explicitly request a different GUI backend:

In[3]:%matplotlibqt

You can list the available backends using the -l/–list option:

In[4]:%matplotlib--listAvailablematplotlibbackends:['osx','qt4','qt5','gtk3','gtk4','notebook','wx','qt','nbagg','gtk','tk','inline']
positional arguments:
gui Name of the matplotlib backend to use such as ‘qt’ or ‘widget’.

If given, the corresponding matplotlib backend is used,otherwise it will be matplotlib’s default (which you can set inyour matplotlib config file).

options:
-l,--list

Show available matplotlib backends

pylab(line='')
%pylab[--no-import-all][gui]

Load numpy and matplotlib to work interactively.

This function lets you activate pylab (matplotlib, numpy andinteractive support) at any point during an IPython session.

%pylab makes the following imports:

importnumpyimportmatplotlibfrommatplotlibimportpylab,mlab,pyplotnp=numpyplt=pyplotfromIPython.displayimportdisplayfromIPython.core.pylabtoolsimportfigsize,getfigsfrompylabimport*fromnumpyimport*

If you pass--no-import-all, the last two* imports will be excluded.

See the %matplotlib magic for more details about activating matplotlibwithout affecting the interactive namespace.

positional arguments:
gui Name of the matplotlib backend to use such as ‘qt’ or

‘widget’. If given, the corresponding matplotlib backend isused, otherwise it will be matplotlib’s default (which youcan set in your matplotlib config file).

options:
--no-import-all

Prevent IPython from performingimport* into theinteractive namespace. You can govern the default behaviorof this flag with the InteractiveShellApp.pylab_import_allconfigurable.