How to change backends in matplotlib / Python

Six years later and I came across a similar issue, when trying to decide which backend was available to use.
Note see Caveats - below

This code snippet works well for me:

import matplotlib
gui_env = ['TKAgg','GTKAgg','Qt4Agg','WXAgg']
for gui in gui_env:
    try:
        print "testing", gui
        matplotlib.use(gui,warn=False, force=True)
        from matplotlib import pyplot as plt
        break
    except:
        continue
print "Using:",matplotlib.get_backend()

Using: GTKAgg

As you can deduce, swapping the backend is as simple as re-importing matplotlib.pyplot after forcing the new backend

matplotlib.use('WXAgg',warn=False, force=True)
from matplotlib import pyplot as plt
print "Switched to:",matplotlib.get_backend()

Switched to: WXAgg

For those still having trouble, this code will print out the:
list of Non Gui backends;
the list of Gui backends;
and then attempt to use each Gui backend to see if it is present and functioning.

import matplotlib
gui_env = [i for i in matplotlib.rcsetup.interactive_bk]
non_gui_backends = matplotlib.rcsetup.non_interactive_bk
print ("Non Gui backends are:", non_gui_backends)
print ("Gui backends I will test for", gui_env)
for gui in gui_env:
    print ("testing", gui)
    try:
        matplotlib.use(gui,warn=False, force=True)
        from matplotlib import pyplot as plt
        print ("    ",gui, "Is Available")
        plt.plot([1.5,2.0,2.5])
        fig = plt.gcf()
        fig.suptitle(gui)
        plt.show()
        print ("Using ..... ",matplotlib.get_backend())
    except:
        print ("    ",gui, "Not found")

Caveats: Changes in matplotlib since version 3.3.0

  • The first parameter of matplotlib.use has been renamed from arg to backend (only relevant if you pass by keyword).
  • The parameter warn of matplotlib.use has been removed. A failure to switch the backend will now always raise an ImportError if force is set; catch that error if necessary.
  • All parameters of matplotlib.use except the first one are now keyword-only.

There is an "experimental" feature :

import matplotlib.pyplot as plt
plt.switch_backend('newbackend')

taken from matplotlib doc.

Switch the default backend to newbackend. This feature is experimental, and is only expected to work switching to an image backend. Eg, if you have a bunch of PostScript scripts that you want to run from an interactive ipython session, you may want to switch to the PS backend before running them to avoid having a bunch of GUI windows popup. If you try to interactively switch from one GUI backend to another, you will explode. Calling this command will close all open windows.


Why not just use the reload built-in function (importlib.reload in Python 3)?

import matplotlib
matplotlib.use('agg')

matplotlib = reload(matplotlib)
matplotlib.use('cairo.png')

So I am not completely sure if this is what you are looking for.

You can change your backend through the matplotlibrc file which contains certain configurations for your matplotlib.

In your script you can put:

matplotlib.rcParams['backend'] = 'TkAgg' 

or something like that to switch between backends.


In my case (Windows 10 + python 3.7), the first answer by @Rolf of Saxony didn't work very well. Instead of trying all the available environments and configuring one of them at the beginning, i.e, just after

    import matplotlib

I had to change the environment from 'Agg' to 'TkAgg' using

    matplotlib.use('TKAgg',warn=False, force=True)

right before the code where I actually plotted, i.e,

    import matplotlib.pyplot as plt
    fig = plt.figure()
    # AND SO ON....