0

I have a python script I want to run via a cronjob, it would create several plots based on some data and save them to a directory on my server. I'm using pandas.plot. The script runs fine on my local machine, and also using a python notebook on the server. However, when running in the terminal with $ python myscript.py I get:

raise RuntimeError('Invalid DISPLAY variable')
RuntimeError: Invalid DISPLAY variable

A similar matplotlib question (not helpful as it's specific to MPL): RuntimeError: Invalid DISPLAY variable. Here the solution seems to route the console output somewhere else.

plt.switch_backend('agg')

or

matplotlib.use('agg')

I'm assuming that the error is related to there being no "display" for the plot to show on. In my script I have the plot being assigned to a variable, yet I can't seem to prevent it printing to console.

for query_metadata, dataframe in query_results.items():

    df = dataframe

    df.reset_index(inplace = True)

    fig = df.plot.area(xticks=df.index, rot = 90, figsize = (20,8))
    fig.set_xticklabels(df['date1'])
    fig.set_title(key)
    fig_ext = fig.get_figure()

    fig_ext.savefig("{0}/{1}.png".format(save_dir, key))
JohnL_10
  • 549
  • 5
  • 15

1 Answers1

1

You already got the solution, just set the backend right before importing pandas:

import matplotlib
matplotlib.use('agg')
import pandas as pd
...
fernandezcuesta
  • 2,390
  • 1
  • 15
  • 32