본문 바로가기
Informations/python

python, Jupyter notebook 한글 깨짐 해결, figure 한글 쓰기

by 지구별 이용자 2023. 6. 23.

주피터를 바로 깔고 나서는 한글이 들어간 그림을 그릴 수 없다.

왜냐면 python matplotlib에 기본 폰트중에 한글 지원 가능한 폰트가 없기 때문이다!

 

그래서 아래와 같이 된다.

 

import matplotlib.pyplot as plt

plt.figure(figsize=(5,5))
x=list(range(10))
y=x
plt.scatter(x,y)
plt.grid()
plt.title('한글을 쓰고싶어!!!')
plt.show()

 

해결은 간단하다. 라이브러리에 한글폰트를 넣어주면 되는것.

 

1) 우선 한글 폰트를 하나 가져오자. 

본인 컴퓨터에 있는걸 가져와도 되고, 적당히 인터넷에서 나눔명조 다운로드를 써서 .~.ttf 파일을 받아온다.

 

2) 본인 matplotlib에 ttf파일 모아놓은 곳이 어디인지 찾자.

보통 linux 시작 위치에서 python설치 폴더를 찾아가 find -name "*ttf" 함수 써서 찾을 수 있다.

(R이 깔려있으면 R 폰트위치도 나와서, matplot의 font저장소를 찾아야 한다.)

필자는 아래 위치에 있었다. 미니콘다를 쓴다면 보통 이 비슷한 곳에 있을것이다.

/user/miniconda3/lib/python3.8/site-packages/matplotlib/mpl-data/fonts/ttf/

 

3) 주피터에서 폰트를 불러온다

import matplotlib.font_manager as fm 함수로 불러올 수 있다.

import matplotlib.font_manager as fm
# 폰트 경로 설정
Fontpath = './user/miniconda3/lib/python3.8/site-packages/matplotlib/mpl-data/fonts/ttf/NanumGothic.ttf'  # 사용할 한글 폰트 파일 경로를 입력해주세요
Fontname = fm.FontProperties(fname=Fontpath).get_name()
plt.rc('font', family=Fontname)


plt.figure(figsize=(5,5))
x=list(range(10))
y=x
plt.scatter(x,y)
plt.grid()
plt.title('한글을 쓰고싶어!!!')
plt.show()

 

한국인으로 당당하게 한글을 쓸 수 있다 ^^