Last active
February 12, 2019 01:06
-
-
Save rhee/0b3cf9b68669c82c50aa0b544d09a1ee to your computer and use it in GitHub Desktop.
Finally, I succeeded to use custom matplotlib Korean font in non-system custom font directory, without installing... thanks https://stackoverflow.com/a/43647344
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# download font from: | |
# https://noto-website-2.storage.googleapis.com/pkgs/NotoSansCJKkr-hinted.zip | |
from __future__ import print_function | |
import sys | |
import os | |
import matplotlib as mpl | |
import matplotlib.font_manager as fm | |
try: | |
_font_notokr_dir = os.path.join( | |
os.path.dirname(os.path.realpath(__file__)), | |
'Fonts/NotoSansCJKkr-hinted') | |
except: | |
_font_notokr_dir = os.path.join( | |
os.path.dirname(os.path.abspath('')), | |
'Fonts/NotoSansCJKkr-hinted') | |
if not os.path.isdir(_font_notokr_dir): | |
from subprocess import Popen as popen | |
try: | |
# python 2 | |
from urllib import urlretrieve | |
except: | |
# python 3 | |
from urllib.request import urlretrieve | |
os.makedirs(_font_notokr_dir) | |
urlretrieve('https://noto-website-2.storage.googleapis.com/pkgs/NotoSansCJKkr-hinted.zip',_font_notokr_dir+'/NotoSansCJKkr-hinted.zip') | |
popen(['unzip','-d',_font_notokr_dir,_font_notokr_dir+'/NotoSansCJKkr-hinted.zip']).wait() | |
fm.fontManager.ttflist.extend(fm.createFontList(fm.findSystemFonts(fontpaths=[_font_notokr_dir]))) | |
_ttfnames = {f.name for f in fm.fontManager.ttflist} | |
if "Noto Sans Korean" in _ttfnames: | |
# mac-OS style | |
mpl.rc('font',family="Noto Sans Korean") | |
print("mpl font: Noto Sans Korean",file=sys.stderr) | |
if "Noto Sans CJK KR" in _ttfnames: | |
# linux style | |
mpl.rc('font',family="Noto Sans CJK KR") | |
print("mpl font: Noto Sans CJK KR",file=sys.stderr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment