Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
matplotlibで日本語フォントを使うサンプル
#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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment