Matplotlib 에서 figure 에 Japanese 를 사용하면 일본어 폰트가 깨져서 한번 사용해 봤음

  • TakaoPGothic font
    • 기본적으로 ubuntu 에 깔려있는 폰트인 TakaoPGothic 을 사용하니 그나마 덜 깨졌음
  • ipag.ttf font
    • 또한 ipag.ttf 폰트를 download 하여 matplotlib.font_manager.FontProperties 로 설정한 후 text 를 적는 부분에서 fontdict={} 로 설정해주니
      그나마 또한 덜 깨졌음
  • ipa download 후 사용
  • 하지만 위의 3 가지 방법 역시 완벽하게 Japanese Font 를 100% 지원하지는 못 함.
  • 방법이 없을까나....
  • 이 방법도 한 번 해봐라

.

.

.

Linux

Install

  • Make a directory under your home directory and put downloaded zip file (In this example, the file name is IPAfont00302.zip) into the directory.
    $ mkdir ~/.fonts
    $ cp IPAfont00302.zip ~/.fonts
  • Move to the directory, unzip the file, and check extracted files.
    $ cd ~/.fonts
    $ unzip IPAfont00302.zip
    $ ls IPAfont00302
  • Refresh font cache of the system.
    $ fc-cache -fv

Uninstall

  • Just delete these files for uninstalling IPA Font.
    $ rm -r ~/.fonts/IPAfont00302

.

.

.

##
# -*- coding: utf-8 -*-

# -------- python modules --------
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
from datetime import datetime

# -------- jmlearn modules --------
import jmlearn.midend.fileutil as M

# -------- set Japanese Font --------
# 이걸 해줘야 unicode 뻑 안 나지
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

# TakaoGothic, TakaoPGothic, TakaoMincho
mpl.rc('font', family='Noto Sans CJK JP')
# mpl.rc('font', family='Arial')
# mpl.rc('font', family='TakaoGothic')
# mpl.rc('font', family='TakaoPGothic')
# mpl.rc('font', family='Takaomincho')
# mpl.rc('font', family='Droid Sans Japanese')
# mpl.rc('font', **{'sans-serif': 'Arial', 'family': 'sans-serif'})
"""Japanese Font 는 일단 다음 두 가지를 사용할 것. Font Crash 에 대해 가장 안정적이었음.
"""
font_prop = mpl.font_manager.FontProperties(fname='/home/juce/Documents/font/ipag.ttf')
# font_prop = mpl.font_manager.FontProperties(fname='/home/juce/Documents/font/IPAexfont00301/ipaexg.ttf')

.

.

.

##
# check font
font_names = []
for i, x in enumerate(mpl.font_manager.fontManager.ttflist):
print '{:3} : type - {}, font`s name - {:30}, font - {}'.format(i, type(x), x.name, x)
font_names.append(x.name)
##
# print font
font_names.sort()
for item in font_names:
print item

##

.

.

.

  • 일본어 폰트가 제대로 보이지 않을 때
    • http://olsgaard.dk/notes/?p=64
      • # -------- set Japanese Font --------
        import sys
        reload(sys)
        sys.setdefaultencoding('utf8') # for unicode

        mpl.rc('font', family='Noto Sans CJK JP') # for Japanese
        # mpl.rc('font', family='TakaoGothic')
        # mpl.rc('font', family='TakaoPGothic')
        # mpl.rc('font', family='Takaomincho') # 얘도 된다고 하는데 사용하지를 못해봤음.
        # TODO - Japanese Font 는 일단 다음 두 가지를 사용할 것. Font Crash 에 대해 가장 안정적이었음.
        font_prop = mpl.font_manager.FontProperties(fname='/home/juce/Documents/font/ipag.ttf')
        # font_prop = mpl.font_manager.FontProperties(fname='/home/juce/Documents/font/IPAexfont00301/ipaexg.ttf')
    • 참고 사이트들


##
# -*- coding: utf-8 -*-
"""
file path 를 설정하여
directory path 가 없으면 os.makedirs 를 이용해서 directory 를 만들어주고
os.utime() 으로 file 을 새로 만든다.
"""
import os

def mkdir_if_not(filepath):
"""
path directory path 부분이 없으면 mkdir 을 한다.
:param filepath: 파일 패쓰
:return: filpath 그대로 리턴
"""

# os.path.split(filepath)
# directory path file name 으로 나누어 return
dirpath, filename = os.path.split(filepath)
print 'Directory Path : ', dirpath
print 'File Name : ', filename

if not dirpath:
return filepath
if not os.path.exists(dirpath):
os.makedirs(dirpath)

return dirpath, filename


def touch(fname, times=None):
# os.utime(path, times)
# Parameters
# path -- This is the path of the file.
# times -- This is the file access and modified time. If times is none,
# then the file access and modified times are set to the current time.
# The parameter times consists of row in the form of (atime, mtime)
# i.e (accesstime, modifiedtime).
fhandle = open(fname, 'a')
try:
# os.utime() 이용해서 linux 에서 touch 하듯 make file.
os.utime(fname, times)
finally:
fhandle.close()


def test():

filepath = '/data/temp/tempfile.txt'
dirpath, filename = mkdir_if_not(filepath)
print 'Directory Path : ', dirpath
print 'File Name : ', filename

touch(filepath)

# p.s.
# os.utime() 사용법
import time
a = time.time() # a : accesstime
b = time.time() + 120 # b : modified time, 120(2) 더해줌.
os.utime(filepath, (a, b)) # (a, b) :


if __name__ == '__main__':
test()





+ Recent posts