본문 바로가기
카테고리 없음

matplotlib, networkx에서 한글사용하기(linux)

by 단창 2020. 1. 21.

https://financedata.github.io/posts/matplotlib-hangul-for-ubuntu-linux.html

 

matplotlib + 한글 (Ubuntu Linux)

matplotlib 에서 한글 (Ubuntu Linux)

financedata.github.io

 

2012년 부터 우분투에는 나눔글꼴이 기본 포함되어 있다.

만일, 나눔 글꼴이 설치되어 있지 않다면 다음과 같이 설치할 수 있으며, 다른 ttf 폰트도 /usr/share/fonts/ 폴더에 복사하여 사용할 수 있다.

나눔 글꼴 설치

apt-get 명령으로 나눔글꼴(fonts-nanum)을 설치하고, fc-cache 명령으로 폰트 캐시 삭제

$ sudo apt-get install fonts-nanum* $ sudo fc-cache -fv

만일 다른 ttf 폰트를 가져왔다면 다음과 같이 복사하고, fc-cache 명령으로 폰트 캐시 삭제

$ sudo cp new_font.ttf /usr/share/fonts/ $ sudo fc-cache -fv

matplotlib 나눔 글꼴을 추가

나눔 글꼴을 matplotlib 에 복사하고, matplotlib의 폰트 캐시를 삭제

사용할 matplotlib 패키지 않에 설치

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

$ rm -rf /home/ubuntu/.cache/matplotlib/*

 

텍스트 출력시 폰트 특성을 지정

텍스트를 출력하는 다음 함수들을 사용할 때, fontproperties 인자에 폰트를 지정할 수 있다.

matplotlib.pyplot

  • title()
  • xlabel()
  • ylabel()
  • legend()
  • text()

matplotlib.axes

  • set_title()

예를 들어, 다음과 같이 텍스트와 폰트를 지정하여 출력할 수 있음

  • plt.text(0, 0, "Sample Text", fontproperties=fontprop)

example)

import matplotlib.pyplot as plt import matplotlib.font_manager as fm

path = '/usr/share/fonts/truetype/nanum/NanumMyeongjo.ttf' 
fontprop = fm.FontProperties(fname=path, size=18) 
plt.plot(range(50), data, 'r') 
plt.title('가격변동 추이', fontproperties=fontprop) 
plt.ylabel('가격', fontproperties=fontprop) 
plt.show()
반응형