While Matplotlib itself runs fine in a virtual environment (venv), some of the GUI frameworks that Matplotlib uses for interactive figures are tricky to install in a venv. Everything below assumes some familiarity with the Matplotlib backends as found in :ref:`What is a backend? <what-is-a-backend>`.
If you only use the IPython and Jupyter Notebook's inline and notebook
backends, or non-interactive backends, you should not have any issues and can
ignore everything below.
Likewise, the Tk framework (TkAgg backend) does not require any
external dependencies and is normally always available. On certain Linux
distributions, a package named python-tk (or similar) needs to be
installed.
Otherwise, the situation (at the time of writing) is as follows:
| framework | bindings | pip-installable? | conda or conda-forge-installable? |
|---|---|---|---|
| Qt5 | PyQt5 | yes | yes |
| Qt5 | PySide2 | yes | yes |
| Qt4 | PyQt4 | no | yes |
| Qt4 | PySide | OSX and Windows | yes |
| GTK3 | PyGObject | yes [1] | Linux and OSX |
| wxWidgets | wxPython | yes [2] | yes |
| [1] | No wheels available, see https://pygobject.readthedocs.io/en/latest/devguide/dev_environ.html for build instructions. |
| [2] | OSX and Windows wheels available on PyPI. Linux wheels available but not on PyPI, see https://wxpython.org/pages/downloads/. |
For cases where the framework is not installable in a venv, it needs to be installed in the global (system) site-packages, and then made available from within the venv. This can be achieved by either of the following methods (in all cases, the system-wide Python and the venv Python must be of the same version):
- vext allows controlled access
from within the venv to specific system-wide packages. A specific
package needs to be installed for each framework, e.g. vext.pyqt5, etc. It is recommended to use
vext>=0.7.0as earlier versions misconfigure the logging system. - Using the
--system-site-packagesoption when creating an environment adds all system-wide packages to the virtual environment. However, this breaks the isolation between the virtual environment and the system install. Among other issues it results in hard to debug problems with system packages shadowing the environment packages. If you use virtualenv <https://virtualenv.pypa.io/> (rather than the stdlib'svenv) together with virtualenvwrapper, this can be toggled with thetoggleglobalsitepackagescommand.