| 1 |
.. _plotting: |
| 2 |
|
| 3 |
Rich Outputs |
| 4 |
------------ |
| 5 |
|
| 6 |
One of the main feature of IPython when used as a kernel is its ability to |
| 7 |
show rich output. This means that object that can be representing as image, |
| 8 |
sounds, animation, (etc...) can be shown this way if the frontend support it. |
| 9 |
|
| 10 |
In order for this to be possible, you need to use the ``display()`` function, |
| 11 |
which you can import with ``from IPython.display import display``. Then use |
| 12 |
``display(<your object>)`` instead of ``print()``, and if possible your object |
| 13 |
will be displayed with a richer representation. In the terminal of course, there |
| 14 |
won't be much difference as objects are most of the time represented by text, |
| 15 |
but in notebook and similar interface you will get richer outputs. |
| 16 |
|
| 17 |
|
| 18 |
.. _matplotlib_magic: |
| 19 |
|
| 20 |
Plotting |
| 21 |
-------- |
| 22 |
|
| 23 |
.. note:: |
| 24 |
|
| 25 |
You can avoid the use of IPython's specific magic and use |
| 26 |
``matplotlib.pyplot.ion()``/``matplotlib.pyplot.ioff()`` which have the |
| 27 |
advantages of working outside of IPython as well. |
| 28 |
|
| 29 |
|
| 30 |
One major feature of the IPython kernel is the ability to display plots that |
| 31 |
are the output of running code cells. The IPython kernel is designed to work |
| 32 |
seamlessly with the matplotlib_ plotting library to provide this functionality. |
| 33 |
|
| 34 |
To set this up, before any plotting or import of matplotlib is performed you |
| 35 |
may execute the ``%matplotlib`` :ref:`magic command <magics_explained>`. This |
| 36 |
performs the necessary behind-the-scenes setup for IPython to work correctly |
| 37 |
hand in hand with ``matplotlib``; it does *not*, however, actually execute any |
| 38 |
Python ``import`` commands, that is, no names are added to the namespace. |
| 39 |
|
| 40 |
If you do not use the ``%matplotlib`` magic or you call it without an argument, |
| 41 |
the output of a plotting command is displayed using the default ``matplotlib`` |
| 42 |
backend, which may be different depending on Operating System and whether |
| 43 |
running within Jupyter or not. |
| 44 |
|
| 45 |
Alternatively, the backend can be explicitly requested using, for example:: |
| 46 |
|
| 47 |
%matplotlib gtk |
| 48 |
|
| 49 |
The argument passed to the ``%matplotlib`` magic command may be the name of any |
| 50 |
backend understood by ``matplotlib`` or it may the name of a GUI loop such as |
| 51 |
``qt`` or ``osx``, in which case an appropriate backend supporting that GUI |
| 52 |
loop will be selected. To obtain a full list of all backends and GUI loops |
| 53 |
understood by ``matplotlib`` use ``%matplotlib --list``. |
| 54 |
|
| 55 |
There are some specific backends that are used in the Jupyter ecosystem: |
| 56 |
|
| 57 |
- The ``inline`` backend is provided by IPython and can be used in Jupyter Lab, |
| 58 |
Notebook and QtConsole; it is the default backend when using Jupyter. The |
| 59 |
outputs of plotting commands are displayed *inline* within frontends like |
| 60 |
Jupyter Notebook, directly below the code cells that produced them. |
| 61 |
The resulting plots will then also be stored in the notebook document. |
| 62 |
|
| 63 |
- The ``notebook`` or ``nbagg`` backend is built into ``matplotlib`` and can be |
| 64 |
used with Jupyter ``notebook <7`` and ``nbclassic``. Plots are interactive so |
| 65 |
they can be zoomed and panned. |
| 66 |
|
| 67 |
- The ``ipympl`` or ``widget`` backend is for use with Jupyter ``lab`` and |
| 68 |
``notebook >=7``. It is in a separate ``ipympl`` module that must be |
| 69 |
installed using ``pip`` or ``conda`` in the usual manner. Plots are |
| 70 |
interactive so they can be zoomed and panned. |
| 71 |
|
| 72 |
.. seealso:: |
| 73 |
|
| 74 |
`Plotting with Matplotlib`_ example notebook |
| 75 |
|
| 76 |
See the matplotlib_ documentation for more information, in particular the |
| 77 |
section on backends. |
| 78 |
|
| 79 |
.. include:: ../links.txt |