RuntimeError: Invalid DISPLAY variable

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

Reference


Add

plt.switch_backend('agg')

after

import matplotlib.pyplot as plt