Python Matplotlib:检查是否显示未定义的内容

Python Matplotlib:检查是否显示未定义的内容,python,matplotlib,ssh,Python,Matplotlib,Ssh,有时我通过ssh运行脚本。让我来安排 import matplotlib #matplotlib.use('Agg') # Must be before importing matplotlib.pyplot or pylab! import matplotlib.pyplot as plt 当我通过ssh运行脚本得到undefined屏幕错误时。然而,有了这个序言,当我在本地机器上运行脚本时,我无法以交互方式查看图形 检查屏幕是否已定义的条件是什么?我想做 if SCREEN == None

有时我通过ssh运行脚本。让我来安排

import matplotlib
#matplotlib.use('Agg') # Must be before importing matplotlib.pyplot or pylab!
import matplotlib.pyplot as plt
当我通过ssh运行脚本得到
undefined屏幕
错误时。然而,有了这个序言,当我在本地机器上运行脚本时,我无法以交互方式查看图形

检查屏幕是否已定义的条件是什么?我想做

if SCREEN == None:
  matplotlib.use('Agg')

正确的代码是什么,我如何检查它?

看起来最简单的方法是检查“DISPLAY”环境变量

import os

# 'DISPLAY' will be something like this ':0'
# on your local machine, and None otherwise
if os.environ.get('DISPLAY') is None:
    matplotlib.use('Agg')
你和我有联系吗?