Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RuntimeError: Invalid DISPLAY variable

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

like image 304
Dalek Avatar asked Mar 02 '16 02:03

Dalek


2 Answers

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

Reference

like image 167
Mauro Baraldi Avatar answered Nov 15 '22 06:11

Mauro Baraldi


Add

plt.switch_backend('agg')

after

import matplotlib.pyplot as plt
like image 55
ssmetkar Avatar answered Nov 15 '22 07:11

ssmetkar