Invalid DISPLAY variable

In my AWS Ubuntu 14.02 instance I installed the matplotlib dependencies and then via pip I installed matplotlib:

sudo apt-get build-dep python-matplotlib

sudo pip install matplotlib

sudo pip freeze:

in a brand new file I wrote:


from pylab import *

after typying chmod +x, I executed the little script:

time python
Traceback (most recent call last):
File “”, line 4, in
File “/usr/local/lib/python2.7/dist-packages/matplotlib/”, line 3086, in plot
ax = gca()
File “/usr/local/lib/python2.7/dist-packages/matplotlib/”, line 827, in gca
ax = gcf().gca(*_kwargs)
File “/usr/local/lib/python2.7/dist-packages/matplotlib/”, line 461, in gcf
return figure()
File “/usr/local/lib/python2.7/dist-packages/matplotlib/”, line 434, in figure
File “/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt4ag”, line 47, in new_figure_manager
return new_figure_manager_given_figure(num, thisFig)
File “/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt4ag”, line 54, in new_figure_manager_given_figure
canvas = FigureCanvasQTAgg(figure)
File “/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt4ag”, line 72, in init
FigureCanvasQT.init(self, figure)
File “/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt4.p y”, line 68, in init
File “/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt5.p y”, line 139, in _create_qApp
raise RuntimeError(‘Invalid DISPLAY variable’)
RuntimeError: Invalid DISPLAY variable

real 0m0.404s
user 0m0.284s

What do I have to do now?

Looking forward to your kind help.
Kind regards.

Author: Fantashit

4 thoughts on “Invalid DISPLAY variable

  1. I had the same problem and I solved it by adding plt.switch_backend('agg') after import matplotlib.pyplot as plt

  2. For the record it’s better to chose a backend before importing pyplot i.e.

    import matplotlib
    import matplotlib.pyplot as plt
  3. For me on AWS ec2 instance

    import matplotlib matplotlib.use('agg')
    Does not work. But
    does the trick.

  4. @hana9090 That is not how the library works. plt.switch_backend() changes some global state which affects how future calls to plt.subplots() works.

Comments are closed.