83

The current backend name is accessible via

>>> import matplotlib.pyplot as plt>>> plt.get_backend()
'GTKAgg'

Is there a way to get a list of all backends that can be used on a particular machine?

Aaron Hall
399k93 gold badges415 silver badges342 bronze badges
asked Feb 23, 2011 at 14:10
1
  • FYI as of matplotlib version 3.x the default pip install does not install an interactive backend so you need to also install one of the interative backends like PyQt4, or PyQt5 see matplotlib.org/3.3.3/users/installing.html#install-requirements for more info about supported interactive backends. Commented Jan 27, 2021 at 16:35

7 Answers 7

64

Matplotlib 3.9 and above has a backend registry that you can use to query backends in various ways:

from matplotlib.backends import backend_registry
# interactive backends
backend_registry.list_builtin(matplotlib.backends.BackendFilter.INTERACTIVE)
# noninteractive backends
backend_registry.list_builtin(matplotlib.backends.BackendFilter.NON_INTERACTIVE)
# all backends built into Matplotlib
backend_registry.list_builtin()
# all registered backends
backend_registry.list_all()

In older versions of Matplotlib you can use the lists

matplotlib.rcsetup.interactive_bk
matplotlib.rcsetup.non_interactive_bk
matplotlib.rcsetup.all_backends

the third being the concatenation of the former two. These will be remove in version 3.11.

answered Feb 23, 2011 at 14:32
53

Here is a modification of the script posted previously. It finds all supported backends, validates them and measures their fps. On OSX it crashes python when it comes to tkAgg, so use at your own risk ;)

from __future__ import print_function, division, absolute_import
from pylab import *
import time
import matplotlib.backends
import matplotlib.pyplot as p
import os.path
def is_backend_module(fname):
 """Identifies if a filename is a matplotlib backend module"""
 return fname.startswith('backend_') and fname.endswith('.py')
def backend_fname_formatter(fname): 
 """Removes the extension of the given filename, then takes away the leading 'backend_'."""
 return os.path.splitext(fname)[0][8:]
# get the directory where the backends live
backends_dir = os.path.dirname(matplotlib.backends.__file__)
# filter all files in that directory to identify all files which provide a backend
backend_fnames = filter(is_backend_module, os.listdir(backends_dir))
backends = [backend_fname_formatter(fname) for fname in backend_fnames]
print("supported backends: \t" + str(backends))
# validate backends
backends_valid = []
for b in backends:
 try:
 p.switch_backend(b)
 backends_valid += [b]
 except:
 continue
print("valid backends: \t" + str(backends_valid))
# try backends performance
for b in backends_valid:
 ion()
 try:
 p.switch_backend(b)
 clf()
 tstart = time.time() # for profiling
 x = arange(0,2*pi,0.01) # x-array
 line, = plot(x,sin(x))
 for i in arange(1,200):
 line.set_ydata(sin(x+i/10.0)) # update the data
 draw() # redraw the canvas
 print(b + ' FPS: \t' , 200/(time.time()-tstart))
 ioff()
 except:
 print(b + " error :(")

To just see supported interactive backends see:

#!/usr/bin/env python
from __future__ import print_function
import matplotlib.pyplot as plt
import matplotlib
backends = matplotlib.rcsetup.interactive_bk
# validate backends
backends_valid = []
for b in backends:
 try:
 plt.switch_backend(b)
 backends_valid += [b]
 except:
 continue
print(backends_valid)
Trevor Boyd Smith
19.4k37 gold badges132 silver badges193 bronze badges
answered Dec 5, 2012 at 19:44
5
  • The scripts crashes for me (gist.github.com/palmstrom/6039823), but works well when run under Spyder IDE. Commented Jul 19, 2013 at 15:12
  • What are typical results of this benchmark? FPS numbers anybody? Commented May 3, 2017 at 11:25
  • 2
    so awesome! live drawing! calculates framerate and prints it! really useful to figure out which backend is fastest on your machine. Commented May 31, 2018 at 16:32
  • 1
    to run the script under python2, make the first line after shebang the from __future__ import print_function, division, absolute_import. (i was going to edit the code in the question... but python2 is so bloody old at this point... i feel it is bad manners to add that sort of cruft... i feel like it would just be encouraging bad behavior.) Commented Jul 11, 2019 at 17:46
  • Nice snippet! Usage of "from pylab import *" is discouraged, though: (matplotlib.org/stable/api/pylab.html). Commented Nov 22, 2023 at 14:08
8

You can pretend to put a wrong backend argument, then it will return you a ValueError with the list of valid matplotlib backends, like this:

Input:

import matplotlib
matplotlib.use('WRONG_ARG')

Output:

ValueError: Unrecognized backend string 'test': valid strings are ['GTK3Agg', 'GTK3Cairo', 'MacOSX', 'nbAgg', 'Qt4Agg', 'Qt4Cairo', 'Qt5Agg', 'Qt
5Cairo', 'TkAgg', 'TkCairo', 'WebAgg', 'WX', 'WXAgg', 'WXCairo', 'agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template']
answered Jun 23, 2019 at 4:54
6

There is the hard-coded list mentioned by Sven, but to find every backend which Matplotlib can use (based on the current implementation for setting up a backend) the matplotlib/backends folder can be inspected.

The following code does this:

import matplotlib.backends
import os.path
def is_backend_module(fname):
 """Identifies if a filename is a matplotlib backend module"""
 return fname.startswith('backend_') and fname.endswith('.py')
def backend_fname_formatter(fname): 
 """Removes the extension of the given filename, then takes away the leading 'backend_'."""
 return os.path.splitext(fname)[0][8:]
# get the directory where the backends live
backends_dir = os.path.dirname(matplotlib.backends.__file__)
# filter all files in that directory to identify all files which provide a backend
backend_fnames = filter(is_backend_module, os.listdir(backends_dir))
backends = [backend_fname_formatter(fname) for fname in backend_fnames]
print backends
answered May 7, 2011 at 10:12
4

You can also see some documentation for a few backends here:

http://matplotlib.org/api/index_backend_api.html

the pages lists just a few backends, some of them don't have a proper documentation:

matplotlib.backend_bases
matplotlib.backends.backend_gtkagg
matplotlib.backends.backend_qt4agg
matplotlib.backends.backend_wxagg
matplotlib.backends.backend_pdf
matplotlib.dviread
matplotlib.type1font
answered Feb 18, 2013 at 12:37
1
  • I'm sorry, I thought that the above answers covered the subject, but should have the link as reference, that's why I posted. I mean no harm, I'm a noob. Commented Mar 12, 2013 at 19:18
4

What about this?

%matplotlib --list
Available matplotlib backends: ['tk', 'gtk', 'gtk3', 'wx', 'qt4', 'qt5', 'qt', 'osx', 'nbagg', 'notebook', 'agg', 'svg', 'pdf', 'ps', 'inline', 'ipympl', 'widget']
answered Oct 2, 2020 at 19:07
2

You could look at the following folder for a list of possible backends...

/Library/Python/2.6/site-packages/matplotlib/backends
/usr/lib64/Python2.6/site-packages/matplotlib/backends
answered Feb 23, 2011 at 14:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.