Matplotlibで日本語フォントを使う

gistを使ってみたかったので、Matplotlibで日本語フォントを使うサンプルを上げます。
http://matplotlib.sourceforge.net

#coding:utf-8
"""
matplotlibで日本語フォントを使うサンプル
"""
import matplotlib.pyplot
import matplotlib.font_manager
# for Mac
font_path = '/Library/Fonts/Osaka.ttf'
# for Linux
#font_path = '/home/k01/.fonts/ipag00303/ipag.ttf'
# font_pathが見つからない場合
# RuntimeError: Could not open facefile
# /home/k01/.fonts/ipag00303/ipag.ttf; Cannot_Open_Resource になる
font_prop = matplotlib.font_manager.FontProperties(fname=font_path)
matplotlib.pyplot.title(u'日本語のテストタイトル', fontproperties=font_prop)
matplotlib.pyplot.plot(range(10))
# matplotlib.pyplot.show()だと日本語は正しく表示されない savefigはOK
matplotlib.pyplot.savefig('test.png')

test

Posted: September 28th, 2011 | Author: | Filed under: 技術 | Tags: , , , | No Comments »