Extra Info
Although everyone show their own favorite back-end, it's quite messy to figure out what you need to install and what the requirements are. At least when you try to understand the generally confusing and technocratic documentation. Today I have made several experiments and think I have found the most simple solutions for each back-end to be able to plot with matplotlib in Windows and using Py3.8.5 (without Qt built-in). Here are my findings:
#--------------------------------------
# Installing the matplotlib backends
#--------------------------------------
# https://matplotlib.org/users/installing.html
pip install pycairo # [1] Cairo : GTK3 based backend (replaces: cairocffi) [1.2 Mb]
#pip install mplcairo # [1] Cairo : Easy & Specific for matplotlib [1.7 Mb]
#pip install PyQt5 # [2] Qt5 : Require: Qt's qmake tool []
pip install PySide2 # [3] Qt5 : Require: shiboken2 & Clang lib bindings [136 Mb]
pip install wxPython # [4] WxAgg : ?? [18.1 Mb]
pip install tornado # [5] WebAgg: Require: pycurl,twisted,pycares ?? [422 kb]
Here are the links to all those PyPi's or Repos:
[1] https://pypi.org/project/pycairo/ # Requires GTK3+: https://www.gtk.org/
[1] https://pypi.org/project/mplcairo/ # https://github.com/matplotlib/mplcairo
[2] https://pypi.org/project/PyQt5/ #
[3] https://pypi.org/project/PySide2/ #
[4] https://pypi.org/project/wxPython/ # https://github.com/wxWidgets/Phoenix/
[4] https://github.com/wxWidgets/wxWidgets/releases
[5] https://pypi.org/project/tornado/ # https://github.com/tornadoweb/tornado
However, and very surprising given the vast quantities of documentation for using Tcl/Tk
and the tkinter
packages for python, I was not able to install it, nor find any useful instructions of how to import the the DLL's into python. (For linux this is trivial as you just use the OS package manager apt-get install <tk-what-not>
, but this option is not available for windows, nor is there any simple analogue. Here is a list of the many and various binaries that you can get.
[6] http://www.bawt.tcl3d.org/download.html # Use Tcl-Pure (no additional packages)
[6] https://www.magicsplat.com/tcl-installer/index.html #
[6] https://www.irontcl.com/ #
[6] https://www.activestate.com/products/tcl/downloads/ #
[6] http://tclkits.rkeene.org/fossil/wiki/Downloads #
[6] http://kitcreator.rkeene.org/kitcreator #
Perhaps someone can enlighten me as how to get any of these imported into python?
What backends are available and where?
Let's enlighten ourselves with the following one-liners:
python.exe -c "import matplotlib as m; help(m);"
python.exe -c "import matplotlib as m; print('I: {}\nN: {}'.format(m.rcsetup.interactive_bk,m.rcsetup.non_interactive_bk));"
# I: ['GTK3Agg', 'GTK3Cairo', 'MacOSX', 'nbAgg', 'Qt4Agg', 'Qt4Cairo', 'Qt5Agg', 'Qt5Cairo', 'TkAgg', 'TkCairo', 'WebAgg', 'WX', 'WXAgg', 'WXCairo']
# N: ['agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template']
python.exe -c "import matplotlib as m; p=m.get_backend();print('current backend is:',p);"
# agg
python.exe -c "import matplotlib as m; p=m.matplotlib_fname(); print('The matplotlibrc is located at:\n',p);"
# C:\Python38\lib\site-packages\matplotlib\mpl-data\matplotlibrc
Setting the back-end
As the documentation says: There are 3 ways to configure your backend:
- Setting the
rcParams["backend"]
(default: 'agg') parameter in your matplotlibrc file
- Setting the
MPLBACKEND
environment (shell) variable
- Using the function
matplotlib.use()
Using the backend
Finally, to use your available backend is just a matter of doing this:
import matplotlib
#matplotlib.use('tkagg', force=True) # Agg rendering to a Tk canvas
#matplotlib.use('wxcairo', force=True) # Cairo rendering to a wxWidgets canvas
#matplotlib.use('wxagg', force=True) # Agg rendering to a wxWidgets canvas
matplotlib.use('webagg', force=True) # On show() will start a tornado server with an interactive figure.
#matplotlib.use('qt5cairo', force=True) # Cairo rendering to a Qt5 canvas
#matplotlib.use('qt5agg', force=True) # Agg rendering to a Qt5 canvas
import matplotlib.pyplot as plt
pyplot.switch_backends()
. It might not work in your case. – Mastiff