首页 文章

docker / matplotlib:RuntimeError:无效的DISPLAY变量

提问于
浏览
2

我尝试了很多解决方法,但都没有奏效 .

我试过 echo $DISPLAY 没有用

错误信息:

环境:请求方法:GET请求URL:http://10.231.xx.xx:8000 / upload / Traceback:文件“/opt/conda/lib/python2.7/site-packages/django/core/handlers/base .py“in get_response 149. response = self.process_exception_by_middleware(e,request)文件”/opt/conda/lib/python2.7/site-packages/django/core/handlers/base.py“in get_response 147. response = wrapped_callback(request,* callback_args,** callback_kwargs)msa_result中的文件“/code/fileUpload_app/views.py”174. result1 = generate_hist(db,** processing_dict)generate_hist中的文件“/code/fileUpload_app/post_processing.py”182 . fig1 = plt.figure()图527中的文件“/opt/conda/lib/python2.7/site-packages/matplotlib/pyplot.py” . ** kwargs)文件“/ opt / conda / lib / python2 . 7 / site-packages / matplotlib / backends / backend_qt4agg.py“in new_figure_manager 46. return new_figure_manager_given_figure(num,thisFig)File”/opt/conda/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py“在new_figure_manager_given_figure 53. canvas = Figur eCanvasQTAgg(图)init 76中的文件“/opt/conda/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py” . FigureCanvasQT.init(self,figure)File“/ opt / conda / lib /在init 68中的python2.7 / site-packages / matplotlib / backends / backend_qt4.py“._create_qApp()文件”/opt/conda/lib/python2.7/site-packages/matplotlib/backends/backend_qt5.py“在_create_qApp中138.引发RuntimeError('Invalid DISPLAY variable')Exception Type:RuntimeError at / upload / msa_result / 1 / Exception Value:无效的DISPLAY变量

我使用docker来托管我的web项目 .

我的代码包括:

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt

def generate_hist(db, **kwargs):


    fig1 = plt.figure()

    hist = mat2[0, 0:pin_num]

    fig1 = plt.hist(hist)

    plt.savefig("fileUpload_app/static/img/result/hist.jpg")

2 回答

  • 0

    原因是我 import matplotlib 之前 import matplotlib . 这导致matplotlib无法在正确的位置运行 .

  • 0

    从这里:RuntimeError: Invalid DISPLAY variable

    import matplotlib.pyplot as plt
    plt.switch_backend('agg')
    

    我在远程机器上使用它,使用ssh和screen,没有X转发 .

相关问题