Changing the plot backend should fix this.
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
https://stackoverflow.com/questions/2801882/generating-a-png...