우분투

[우분투] Matplotlib 한글 폰트 설정

IntegerString 2020. 10. 19. 11:00

나눔 폰트 설치

sudo apt-get install fonts-nanum*

시스템에서 폰트 업데이트

fc-cache -fv

Matplotlib 패키지 안에 폰트 복사 (본인의 파이썬 버전에 맞게 수정)

cp /usr/share/fonts/truetype/nanum/Nanum* /usr/local/lib/python3.8/dist-packages/matplotlib/mpl-data/fonts/ttf/

font_manager를 이용하여 사용 가능한 나눔 폰트 경로와 파일명을 확인

from matplotlib import font_manager as fm

nanum_font_list = fm.findSystemFonts(fontpaths='/usr/share/fonts/truetype/nanum/')

for font in font_list:
    print(f"Name: {fm.FontProperties(fname=font).get_name()} | Path: {font}")

폰트 적용 방법 1

from matplotlib import rc

rc('font', family='NanumGothic')

폰트 적용 방법 2

from matplotlib import font_manager as fm

fm.get_fontconfig_fonts()
font_path = "/usr/share/fonts/truetype/nanum/NanumGothicCoding.ttf"
font_prop = fm.FontProperties(fname=font_path)

후에 텍스트를 입력하는 함수(plt.title, plt.text 등)에 argument로 fontproperties=font_prop 추가