1

I am trying to plot a pandas series in python. However, rather than working from my home computer I am working via grid computing on a linux shell. When I type:

series.plot()

I get this error:

  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.15.0-py2.7-linux-x86_64.egg/pandas/tools/plotting.py", line 2487, in plot_series
    **kwds)
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.15.0-py2.7-linux-x86_64.egg/pandas/tools/plotting.py", line 2293, in _plot
    plot_obj.generate()
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.15.0-py2.7-linux-x86_64.egg/pandas/tools/plotting.py", line 919, in generate
    self._setup_subplots()
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.15.0-py2.7-linux-x86_64.egg/pandas/tools/plotting.py", line 952, in _setup_subplots
    fig = self.plt.figure(figsize=self.figsize)
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 343, in figure
    **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
    window = Tk.Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1688, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

Does anyone know what I can do or what I should ask my sys admin to do? Is it possible to just save the plot to a file without having it in some sort of display environment first?

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
wolfsatthedoor
  • 7,163
  • 18
  • 46
  • 90

1 Answers1

2

You could reset the matplotib backend to one that does not require a display, such as AGG, PNG, SVG, PDF, PS, Cairo, or GDK:

import numpy as np
import pandas as pd

def reset_backend(backend):
    import sys
    del sys.modules['matplotlib.backends']
    del sys.modules['matplotlib.pyplot']
    import matplotlib as mpl
    mpl.use(backend)  # do this before importing pyplot
    import matplotlib.pyplot as plt
    return plt

reset_backend('agg')
ser = pd.Series(np.random.random(5))
ser.plot()
plt.savefig('/path/to/file.png')

Note that what backends you have available depends on how your installation of matplotlib was built. If you call reset_backend with a backend that is not available, you will see an error message which lists the backends which are available on your installation.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677