<Python, matplotlib> GUIの無い環境でグラフを保存する技。

import matplotlib
matplotlib.use('Agg')

を最初に唱える。 他の全ての描画関係のライブラリより先に唱える必要あり。

こんか感じにしておいた。

  1 #!/usr/bin/env python
  2
  3 from optparse import OptionParser

  8 #import matplotlib.pyplot as plt
  9 #import seaborn as sns
 10
 11 usage = 'usage: %prog [options] args'
 12 parser = OptionParser(usage)

 16 parser.add_option('--noGUI', dest='noGUI',    default=False,         action='store_true', help='Set no GUI condition, default=False')
 17
 18 (options, args) = parser.parse_args()
 19
 20 if options.noGUI:
 21     import matplotlib
 22     matplotlib.use('Agg')
 23
 24 import matplotlib.pyplot as plt
 25 import seaborn as sns

ウインドが無い時のイメージの作り方。 by matplotlib

How-To — Matplotlib 1.4.3 documentation