RuntimeError: Invalid DISPLAY variable

Each Answer to this Q is separated by one/two green lines.

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 "", line 513, in <module>
    fig = plt.figure()
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/", line 435, in figure
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/", line 47, in new_figure_manager
    return new_figure_manager_given_figure(num, thisFig)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/", line 54, in new_figure_manager_given_figure
    canvas = FigureCanvasQTAgg(figure)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/", line 72, in __init__
    FigureCanvasQT.__init__(self, figure)
  File "/vol/aibn84/data2/zahra/anaconda/lib/python2.7/site-packages/matplotlib/backends/", line 68, in __init__
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/", line 138, in _create_qApp
    raise RuntimeError('Invalid DISPLAY variable')
RuntimeError: Invalid DISPLAY variable

any suggestion how to fix the problem

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





import matplotlib.pyplot as plt

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .