RuntimeError: Invalid DISPLAY variable

70,715

Solution 1

You must declare matplotlib.use('agg') before import pylab as plt.

Reference

Solution 2

Add

plt.switch_backend('agg')

after

import matplotlib.pyplot as plt
Share:
70,715
Dalek
Author by

Dalek

Updated on July 09, 2022

Comments

  • Dalek
    Dalek almost 2 years

    I am running my python script in another machine by using ssh command in linux. I have also run this command :

    source ~/.bashrc 
    

    after logging in the other machine, in order to define the proper paths in the new machine. I was getting the error message for running the following python code lines even I have tried to follow the instruction in this question by defining the backend.

    >>> import matplotlib
    >>> import pylab as plt
    >>> matplotlib.use('Agg')
    >>> import numpy as np
    >>> x=np.arange(0,2,0.001)
    >>> y=np.sin(x)**2+4*np.cos(x)
    >>> fig = plt.figure()
    >>> plt.plot(x,y,'r.')     
    

    The error message

    This probably means that Tcl wasn't installed properly.
    Traceback (most recent call last):
      File "Systematic_Optimised.py", line 513, in <module>
        fig = plt.figure()
      File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 435, in figure
        **kwargs)
      File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 47, in new_figure_manager
        return new_figure_manager_given_figure(num, thisFig)
      File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 54, in new_figure_manager_given_figure
        canvas = FigureCanvasQTAgg(figure)
      File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 72, in __init__
        FigureCanvasQT.__init__(self, figure)
      File "/vol/aibn84/data2/zahra/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt4.py", line 68, in __init__
        _create_qApp()
      File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt5.py", line 138, in _create_qApp
        raise RuntimeError('Invalid DISPLAY variable')
    RuntimeError: Invalid DISPLAY variable
    

    any suggestion how to fix the problem

  • Hana90
    Hana90 about 7 years
    still I have the runtime error :( File "/home/user/anaconda2/lib/python2.7/site-packages/matplotlib‌​/backends/backend_qt‌​5.py", line 138, in _create_qApp raise RuntimeError('Invalid DISPLAY variable') RuntimeError: Invalid DISPLAY variable
  • Mauro Baraldi
    Mauro Baraldi about 7 years
    Take a look at this comment
  • Michelle Owen
    Michelle Owen almost 7 years
    it seems you should use lower case 'a', so do matplotlib.use('agg') before import pylab as plt.
  • Mauro Baraldi
    Mauro Baraldi almost 7 years
    @MichelleOwen have you tested? I cannot test it now!
  • Michelle Owen
    Michelle Owen almost 7 years
    @MauroBaraldi yes, I did. At least it worked for me.
  • fr_andres
    fr_andres almost 7 years
    worked for me too in a virtual environment after having to pip install PySide. Thanks a lot!
  • teichert
    teichert almost 7 years
    For me, import matplotlib; matplotlib.use('agg') didn't fix the error, but import matplotlib; matplotlib.pyplot.switch_backend('agg') did (as suggested here).
  • Hugh Perkins
    Hugh Perkins almost 6 years
    Thanks! This plays much more nicely with pylint :)
  • pacificgilly1992
    pacificgilly1992 almost 6 years
    This is fantastic, now I can switch the backends depending on how I run the code. I have a problem when using 'agg' which stops showing the plot in the command window which can sometimes be disappointing. Therefore, I can switch out the backend for my uses.