How can I set the matplotlib 'backend'?
Asked Answered
F

8

123

I am new user of matplotlib, my platform is Ubuntu 10.04 Python 2.6.5

This is my code

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt 
plt.plot([1,2,3])

The error is:

/usr/local/lib/python2.6/dist-packages/matplotlib/backends/__init__.py:41: UserWarning: 
Your currently selected backend, 'agg' does not support show().
Please select a GUI backend in your matplotlibrc file ('/usr/local/lib/python2.6/dist-packages/matplotlib/mpl-data/matplotlibrc')
or with matplotlib.use()
  (backend, matplotlib.matplotlib_fname()))
  • I installed the Anti-Grain Geometry library apt-get install libagg but it is doesn't work.
  • I tried to use other argument of backend like 'GTK' and 'TkAgg'.
  • I installed python-gtk2-dev package, but still the error is below.
  • Can anyone tell me an executable backend argument and its dependency library?

Here is the error:

>>> matplotlib.use('GTK')
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/matplotlib/pyplot.py", line 95, in <module>
    new_figure_manager, draw_if_interactive, show = pylab_setup()
  File "/usr/local/lib/python2.6/dist-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
    globals(),locals(),[backend_name])
  File "/usr/local/lib/python2.6/dist-packages/matplotlib/backends/backend_gtk.py", line 28, in <module>
    from matplotlib.backends.backend_gdk import RendererGDK, FigureCanvasGDK 
  File "/usr/local/lib/python2.6/dist-packages/matplotlib/backends/backend_gdk.py", line 29, in <module>
    from matplotlib.backends._backend_gdk import pixbuf_get_pixels_array
ImportError: No module named _backend_gdk
Fierce answered 8/2, 2011 at 7:17 Comment(0)
S
120

Your currently selected backend, 'agg' does not support show().

AGG backend is for writing to file, not for rendering in a window. See the backend FAQ at the matplotlib web site.

ImportError: No module named _backend_gdk

For the second error, maybe your matplotlib distribution is not compiled with GTK support, or you miss the PyGTK package. Try to install it.

Do you call the show() method inside a terminal or application that has access to a graphical environment?

Try other GUI backends, in this order:

  • TkAgg
  • wxAgg
  • Qt5Agg
  • Qt4Agg
Sidwel answered 8/2, 2011 at 8:15 Comment(5)
thank you very much the wrong staff I did is setup matplotlib before get PyGTKFierce
as a side comment, I would try QTAgg last as that targets QT3 and will likely be deprecated in the mid-term.Stipe
Why are you specifically saying 'in this order' when instructing to use other GUI backends? What are the differences amongst the backend options?Jeane
'Qt5Agg' worked for me (python 3.8)Marybellemarybeth
Depending on your operating system, you may need to install the appropriate libraries for the backends first (e.g. tk on arch linux for TkAgg).Eduard
I
42

FYI, I found I needed to put matplotlib.use('Agg') first in Python import order. For what I was doing (unit testing needed to be headless) that meant putting

import matplotlib
matplotlib.use('Agg')

at the top of my master test script. I didn't have to touch any other files.

Intermix answered 27/11, 2013 at 17:44 Comment(3)
I must do the same thing which troubles me a lot. Have you ever find another elegrant solution?Kilocycle
And I found that it's important for it to be at the very top (not after certain other imports). +1.Recount
Yes, this has to be the absolute first thing you do before any other matplotlib imports.Amagasaki
Y
21

This can also be set in the configuration file matplotlibrc (as explained in the error message), for instance:

# The default backend; one of GTK GTKAgg GTKCairo GTK3Agg GTK3Cairo
# CocoaAgg MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG
backend : Agg

That way, the backend does not need to be hardcoded if the code is shared with other people. For more information, check the documentation.

Yarber answered 15/12, 2015 at 16:12 Comment(0)
D
7

The errors you posted are unrelated. The first one is due to you selecting a backend that is not meant for interactive use, i.e. agg. You can still use (and should use) those for the generation of plots in scripts that don't require user interaction.

If you want an interactive lab-environment, as in Matlab/Pylab, you'd obviously import a backend supporting gui usage, such as Qt4Agg (needs Qt and AGG), GTKAgg (GTK an AGG) or WXAgg (wxWidgets and Agg).

I'd start by trying to use WXAgg, apart from that it really depends on how you installed Python and matplotlib (source, package etc.)

Denten answered 8/2, 2011 at 8:1 Comment(0)
I
6

Before starting python, you can do in bash

export MPLBACKEND=TkAgg
Impressionist answered 21/11, 2018 at 19:57 Comment(0)
R
6

You can also try viewing the graph in a browser.

Use the following:

matplotlib.use('WebAgg')
Rolling answered 27/12, 2019 at 15:17 Comment(1)
previous solutions didn't work for me but this did it first try, thanks!Mcavoy
N
4

For new comers,

matplotlib.pyplot.switch_backend(newbackend)
Nielsen answered 10/11, 2020 at 17:19 Comment(0)
N
1

I hit this when trying to compile python, numpy, scipy, matplotlib in my own VIRTUAL_ENV

Before installing matplotlib you have to build and install: pygobject pycairo pygtk

And then do it with matplotlib: Before building matplotlib check with 'python ./setup.py build --help' if 'gtkagg' backend is enabled. Then build and install

Before export PKG_CONFIG_PATH=$VIRTUAL_ENV/lib/pkgconfig

Notum answered 17/9, 2013 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.