2

Sometimes I run my script via ssh. This answer told me to set up

import matplotlib
#matplotlib.use('Agg') # Must be before importing matplotlib.pyplot or pylab!
import matplotlib.pyplot as plt

when I get the undefined SCREEN error by running the script via ssh. However with that preamble I cannot view the graphs interactively when I run the script on my local machine.

What's the condition to check if the screen is defined? I'd like to do

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

How's the proper code for that, how can I check this?

Community
  • 1
  • 1
FooBar
  • 15,724
  • 19
  • 82
  • 171

1 Answers1

5

It looks like an easiest way to do this is to check 'DISPLAY' environment variable

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')
erthalion
  • 3,094
  • 2
  • 21
  • 28