Matplotlib – Python Error

Question:

Setup:

Processor : x86_64
Operating System: GNU/Linux
Distro: Fedora Release 13 (Goddard).

Python 2.6.4

 /usr/bin/python

Here is the following issue:

I am trying to generate a few graphs using Matplotlib and Python. As I hear from a majority of the solutions3.. there seem to be compatibility issues between matplotlib and 64bit architectures.

I have installed Matplotlib from here

Here is the error that I get:

Traceback (most recent call last):
  File "plot-thread-characterization.py", line 24, in <module>
    import matplotlib.pyplot as plt
  File "/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py", line 78, in <module>
    new_figure_manager, draw_if_interactive, show = pylab_setup()
  File "/usr/lib64/python2.6/site-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
    globals(),locals(),[backend_name])
  File "/usr/lib64/python2.6/site-packages/matplotlib/backends/backend_gtkagg.py", line 10, in <module>
    from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,
  File "/usr/lib64/python2.6/site-packages/matplotlib/backends/backend_gtk.py", line 8, in <module>
    import gtk; gdk = gtk.gdk
  File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 64, in <module>
    _init()
  File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 52, in _init
    _gtk.init_check()
RuntimeError: could not open display

Thanks.

Asked By: user1816896

||

Answers:

matplotlib is failing to connect to any X server for its GTK display.

There are a couple of options here:

  1. Run a local X server and enable X11 forwarding in your ssh client, to display the output on your local machine. You can verify this is working by checking that the $DISPLAY environment variable is set on the server.

  2. Call matplotlib.use(…) to specify a different display back-end, for example rendering to pdf file, before importing pyplot, e.g.

    import matplotlib as mpl

    mpl.use('Agg')

    import matplotlib.pyplot as plt

See http://matplotlib.org/faq/howto_faq.html#generate-images-without-having-a-window-appear for more details.

Answered By: James Beilby
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.