Skip to content

Instantly share code, notes, and snippets.

@ina111
Last active March 21, 2016 12:04
Show Gist options
  • Save ina111/91b91902072c3332a6d3 to your computer and use it in GitHub Desktop.
Save ina111/91b91902072c3332a6d3 to your computer and use it in GitHub Desktop.
MacでMatplotlibで日本語の画像とPDFを作る
# -*- coding: utf-8 -*-
"""
Mac OSXのmatplotlibで日本語フォント(Osaka)を使うサンプル
python2.7(anaconda-2.1.0)
cf. http://matplotlib.org/users/customizing.html
"""
# デフォルトの文字コードを変更する.
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
# import
import matplotlib.pyplot as plt
import matplotlib.font_manager
from matplotlib.font_manager import FontProperties
from matplotlib.backends.backend_pdf import PdfPages
# for Mac
font_path = '/Library/Fonts/Osaka.ttf'
font_prop = matplotlib.font_manager.FontProperties(fname=font_path)
matplotlib.rcParams['font.family'] = font_prop.get_name()
# pdfのフォントをTrueTypeに変更
matplotlib.rcParams['pdf.fonttype'] = 42
# defaultのdpi=100から変更
matplotlib.rcParams['savefig.dpi'] = 300
# 数式(Latex)のフォントを変更
matplotlib.rcParams['mathtext.default'] = 'regular'
pdf = PdfPages("test.pdf")
plt.figure()
plt.plot(range(10))
plt.title(u'日本語のテスト')
plt.xlabel(u'Latexのテスト' + r' $y=a x^{2} \alpha \beta $')
plt.ylabel(u'y軸 (m)')
plt.savefig('test.png')
pdf.savefig()
pdf.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment