RuntimeError:无效的DISPLAY变量

时间:2016-03-02 02:05:55

标签: python linux matplotlib

我在ssh中使用linux命令在另一台机器上运行我的python脚本。我也运行了这个命令:

source ~/.bashrc 
登录其他计算机后,

以便在新计算机中定义正确的路径。我收到了运行以下python代码行的错误消息,即使我已经尝试通过定义后端来遵循this question中的指令。

>>> 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.')     

错误消息

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

任何建议如何解决问题

2 个答案:

答案 0 :(得分:54)

您必须在matplotlib.use('agg')之前声明import pylab as plt

Reference

答案 1 :(得分:39)

添加

plt.switch_backend('agg')

import matplotlib.pyplot as plt