2.5. Mathematical Plots

Attention

Matplotlib does not support labels and auto-references. You can not refer to a equation and you will never see an entry to .. mathmpl:: expressions in the list of equations.

PyPI Package

https://pypi.org/project/matplotlib/

Project Home

https://matplotlib.org/

Documentation

https://matplotlib.org/contents.html

Git Repository

https://github.com/matplotlib/matplotlib

Documentation

https://matplotlib.org/sampledoc/index.html

Git Repository

https://github.com/matplotlib/sampledoc

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. It consists:

Todo

activate “Mathematical Plots” extension.

2.5.1. Expressions

See the Writing mathematical expressions for lots more information how to writing mathematical expressions in matplotlib.

With matplotlib in |Sphinx| you can include inline math
:mathmpl:`(\alpha^{ic} > \beta_{ic})` (as role
:rst:`:mathmpl:`(\alpha^{ic} > \beta_{ic})``) or display math:

.. mathmpl::

   \sum_{i=0}^\infty x_i
.. mathmpl::
The example
1
2
3
.. mathmpl::

   \left(\frac{5 - \frac{1}{x}}{4}\right)
:which gives:

   .. include:: matplotlib/mathmpl/example.rsti

2.5.2. Plots

.. plot::

See the matplotlib Pyplot tutorial and the Gallery for lots of examples of matplotlib plots.

The source code for the plot may be included in one of three ways:

inline content

the example
1
2
3
4
5
6
7
8
9
.. plot::
   :align: center
   :scale: 75

   import matplotlib.pyplot as plt
   import matplotlib.image as mpimg
   import numpy as np
   img = mpimg.imread('https://github.com/matplotlib/matplotlib/raw/master/doc/_static/stinkbug.png')
   imgplot = plt.imshow(img)
:which gives:

   .. include:: matplotlib/inline/example.rsti

doctest content

the example
1
2
3
4
5
6
7
8
.. plot::
   :format: doctest
   :align: center
   :scale: 75

   >>> import matplotlib.pyplot as plt
   >>> plt.plot([1, 2, 3], [4, 5, 6])  # doctest: +ELLIPSIS
   [<matplotlib.lines.Line2D object at 0x...>]
:which gives:

   .. include:: matplotlib/doctest/example.rsti

source file content

When a path to a source file is given, the Sphinx configuration option plot_basedir will respect. It is the base directory, to which .. plot:: file names are relative to. If None or empty, file names are relative to the directory where the file containing the directive is.

.. ifconfig:: not plot_basedir

   :plot_basedir: **None or empty**, file names are **relative**

.. ifconfig:: plot_basedir

   :plot_basedir: currently set to :file:`{plot_basedir}`.
the example
1
2
3
4
5
6
.. plot:: ellipses.py
   :include-source:
   :encoding: utf
   :format: python
   :align: center
   :scale: 75
:which gives:

   .. include:: matplotlib/srcfile/example.rsti

3D-Plots

See mplot3d, mplot3d FAQ, and mplot3d API.

the example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
.. plot::
   :format: python
   :align: center
   :scale: 75

   import matplotlib.pyplot as plt
   from matplotlib import cm
   from mpl_toolkits.mplot3d import axes3d

   fig = plt.figure()
   ax = fig.gca(projection='3d')
   X, Y, Z = axes3d.get_test_data(0.005)
   ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
   cset = ax.contourf(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)
   cset = ax.contourf(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)
   cset = ax.contourf(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)

   ax.set_xlabel('X'); ax.set_xlim(-40, 40)
   ax.set_ylabel('Y'); ax.set_ylim(-40, 40)
   ax.set_zlabel('Z'); ax.set_zlim(-100, 100)

   plt.show()
:which gives:

   .. include:: matplotlib/mplot3d/example.rsti