matplotlib 그림에서 한글 폰트사용하기.

  • 사용환경 : windows 7 64bit, python 2.7.9 64bit, matplotlib 1.4.2

matplotlib 에서 한글 사용시 한글 폰트가 깨짐.

In [1]:
%matplotlib inline
In [2]:
import matplotlib

matplotlib.__version__
Out[2]:
'1.4.2'
In [3]:
# 설치된 TTF 폰트 이름 구하기

import matplotlib.font_manager

for i, x in enumerate(matplotlib.font_manager.fontManager.ttflist):
    print i, x, type(x) , x.name    # x.name  == font name (family name)
    if i == 10: break   # 10개만 출력하기
0 <Font 'Bitstream Vera Serif' (VeraSe.ttf) normal normal 400 normal> <class 'matplotlib.font_manager.FontEntry'> Bitstream Vera Serif
1 <Font 'cmss10' (cmss10.ttf) normal normal 400 normal> <class 'matplotlib.font_manager.FontEntry'> cmss10
2 <Font 'STIXGeneral' (STIXGeneralItalic.ttf) italic normal 400 normal> <class 'matplotlib.font_manager.FontEntry'> STIXGeneral
3 <Font 'STIXSizeFourSym' (STIXSizFourSymBol.ttf) normal normal 700 normal> <class 'matplotlib.font_manager.FontEntry'> STIXSizeFourSym
4 <Font 'cmsy10' (cmsy10.ttf) normal normal 400 normal> <class 'matplotlib.font_manager.FontEntry'> cmsy10
5 <Font 'cmex10' (cmex10.ttf) normal normal 400 normal> <class 'matplotlib.font_manager.FontEntry'> cmex10
6 <Font 'Bitstream Vera Sans Mono' (VeraMoBI.ttf) oblique normal 700 normal> <class 'matplotlib.font_manager.FontEntry'> Bitstream Vera Sans Mono
7 <Font 'STIXSizeOneSym' (STIXSizOneSymBol.ttf) normal normal 700 normal> <class 'matplotlib.font_manager.FontEntry'> STIXSizeOneSym
8 <Font 'STIXNonUnicode' (STIXNonUniBolIta.ttf) italic normal 700 normal> <class 'matplotlib.font_manager.FontEntry'> STIXNonUnicode
9 <Font 'STIXSizeThreeSym' (STIXSizThreeSymBol.ttf) normal normal 700 normal> <class 'matplotlib.font_manager.FontEntry'> STIXSizeThreeSym
10 <Font 'STIXSizeFourSym' (STIXSizFourSymReg.ttf) normal normal 400 normal> <class 'matplotlib.font_manager.FontEntry'> STIXSizeFourSym
In [4]:
import matplotlib.font_manager

myfnt =  matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
print len(myfnt)
print myfnt[:10]  # 10개만 출력하기
1194
[u'C:\\Windows\\Fonts\\cour.ttf', u'C:\\Windows\\Fonts\\HMKMMAG.TTF', u'C:\\Windows\\Fonts\\MP.TTF', u'C:\\Windows\\Fonts\\upcebi.ttf', u'c:\\windows\\fonts\\enorat10.ttf', u'c:\\windows\\fonts\\ftltlt.ttf', u'c:\\windows\\fonts\\browai.ttf', u'C:\\Windows\\Fonts\\HANYGO240.ttf', u'c:\\windows\\fonts\\cambriab.ttf', u'C:\\Windows\\Fonts\\verdanaz.ttf']
In [5]:
import matplotlib.font_manager as fm

for i, x in enumerate(fm.fontManager.ttflist):
    print i, x, type(x) , x.name , x.fname   # x.name  == font name (family name)

    fp1=fm.FontProperties(fname= x.fname).get_name()  # font famiily name
    print fp1
    
    if i == 10: break   # 10개만 출력하기
0 <Font 'Bitstream Vera Serif' (VeraSe.ttf) normal normal 400 normal> <class 'matplotlib.font_manager.FontEntry'> Bitstream Vera Serif C:\Python27\lib\site-packages\matplotlib\mpl-data\fonts\ttf\VeraSe.ttf
Bitstream Vera Serif
1 <Font 'cmss10' (cmss10.ttf) normal normal 400 normal> <class 'matplotlib.font_manager.FontEntry'> cmss10 C:\Python27\lib\site-packages\matplotlib\mpl-data\fonts\ttf\cmss10.ttf
cmss10
2 <Font 'STIXGeneral' (STIXGeneralItalic.ttf) italic normal 400 normal> <class 'matplotlib.font_manager.FontEntry'> STIXGeneral C:\Python27\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXGeneralItalic.ttf
STIXGeneral
3 <Font 'STIXSizeFourSym' (STIXSizFourSymBol.ttf) normal normal 700 normal> <class 'matplotlib.font_manager.FontEntry'> STIXSizeFourSym C:\Python27\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXSizFourSymBol.ttf
STIXSizeFourSym
4 <Font 'cmsy10' (cmsy10.ttf) normal normal 400 normal> <class 'matplotlib.font_manager.FontEntry'> cmsy10 C:\Python27\lib\site-packages\matplotlib\mpl-data\fonts\ttf\cmsy10.ttf
cmsy10
5 <Font 'cmex10' (cmex10.ttf) normal normal 400 normal> <class 'matplotlib.font_manager.FontEntry'> cmex10 C:\Python27\lib\site-packages\matplotlib\mpl-data\fonts\ttf\cmex10.ttf
cmex10
6 <Font 'Bitstream Vera Sans Mono' (VeraMoBI.ttf) oblique normal 700 normal> <class 'matplotlib.font_manager.FontEntry'> Bitstream Vera Sans Mono C:\Python27\lib\site-packages\matplotlib\mpl-data\fonts\ttf\VeraMoBI.ttf
Bitstream Vera Sans Mono
7 <Font 'STIXSizeOneSym' (STIXSizOneSymBol.ttf) normal normal 700 normal> <class 'matplotlib.font_manager.FontEntry'> STIXSizeOneSym C:\Python27\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXSizOneSymBol.ttf
STIXSizeOneSym
8 <Font 'STIXNonUnicode' (STIXNonUniBolIta.ttf) italic normal 700 normal> <class 'matplotlib.font_manager.FontEntry'> STIXNonUnicode C:\Python27\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXNonUniBolIta.ttf
STIXNonUnicode
9 <Font 'STIXSizeThreeSym' (STIXSizThreeSymBol.ttf) normal normal 700 normal> <class 'matplotlib.font_manager.FontEntry'> STIXSizeThreeSym C:\Python27\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXSizThreeSymBol.ttf
STIXSizeThreeSym
10 <Font 'STIXSizeFourSym' (STIXSizFourSymReg.ttf) normal normal 400 normal> <class 'matplotlib.font_manager.FontEntry'> STIXSizeFourSym C:\Python27\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXSizFourSymReg.ttf
STIXSizeFourSym

matplotlib 그림 크기 설정하기

http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure

figsize -- w,h tuple in inches

dpi -- Dots per inch

example : http://matplotlib.org/users/text_intro.html

In [6]:
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import matplotlib


fig = plt.figure()

fig.suptitle('bold figure suptitle', fontsize=14, fontweight='bold')

ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.85)
ax.set_title('axes title')

ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')

ax.text(3, 8, 'boxed italics text in data coords', style='italic',
        bbox={'facecolor':'red', 'alpha':0.5, 'pad':10})

ax.text(2, 6, r'an equation: $E=mc^2$', fontsize=15)


msg = unicode('colored 한글 입력 테스트 in axes coords','utf-8')
# msg = 'colored 한글 in axes coords'

ax.text(0.95, 0.01, msg,
        verticalalignment='bottom', horizontalalignment='right',
        transform=ax.transAxes,
        color='green', fontsize=15)   # 한글 출력 깨져서 나온다...




ax.plot([2], [1], 'o')
ax.annotate('annotate', xy=(2, 1), xytext=(3, 4),
            arrowprops=dict(facecolor='black', shrink=0.05))

ax.axis([0, 10, 0, 10])

plt.show()

한글 깨져서 나온다.

한글 폰트를 설정하고, 그림 크기를 키우자...

In [7]:
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import matplotlib

matplotlib.rc('font', family='HYsanB')  # 한글 폰트 설정

# fig = plt.figure()
fig = plt.figure(figsize=(10, 10), dpi=100)  # 그림 사이즈, 해상도 설정

fig.suptitle('bold figure suptitle', fontsize=14, fontweight='bold')

ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.85)
ax.set_title('axes title')

ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')

ax.text(3, 8, 'boxed italics text in data coords', style='italic',
        bbox={'facecolor':'red', 'alpha':0.5, 'pad':10})

ax.text(2, 6, r'an equation: $E=mc^2$', fontsize=15)


msg = unicode('colored 한글 입력 테스트 in axes coords','utf-8')
# msg = 'colored 한글 in axes coords'

ax.text(0.95, 0.01, msg,
        verticalalignment='bottom', horizontalalignment='right',
        transform=ax.transAxes,
        color='green', fontsize=15)


ax.plot([2], [1], 'o')
ax.annotate('annotate', xy=(2, 1), xytext=(3, 4),
            arrowprops=dict(facecolor='black', shrink=0.05))

ax.axis([0, 10, 0, 10])

plt.show()

** 한글 출력 성공!!!


Posted by 자유프로그램