MatplotlibでDISPLAYが設定されていない場合の描画

リモートサーバなどでMatplotlibを利用したグラフの描画をする場合に, いちいちX Windowを飛ばすのは無駄すぎるのでshowではなくsavefigをDISPLAYを利用することなく実行したい.

結論だけ言うとバックエンドを以下のようにして切り替えればいける.

import matplotlib
# Force matplotlib to not use any Xwindows backend.
matplotlib.use('Agg')

http://stackoverflow.com/questions/2801882/generating-a-png-with-matplotlib-when-display-is-undefined