Programming Language/Python

matplotlib에서 한글이 안써질 때

바나나인간 2018. 10. 24. 10:18

Python에서 matplot을 이용해 그림을 출력하고자 하는데,

plot에 한글이 안써지는 경우가 발생했다.


1
2
3
4
5
import matplotlib.pyplot as plt
 
plt.figure()
plt.title('안녕하세요')
 

cs




해결 방법은 다음과 같다.


1. 한국어 폰트 설치

apt-get install -y fonts-nanum fonts-nanum-coding fonts-nanum-extra


2. 설치 확인

fc-list 


3. python 실행


4. 하단 코드 실행 (1회만 수행하면 모든 프로젝트에 적용)

1
2
3
4
5
import matplotlib as mpl
import matplotlib.font_manager as fm
 
fm._rebuild()
 
cs


5. figure 실행 부분에 하단 코드 삽입

1
2
3
4
5
6
7
8
import matplotlib as mpl
import matplotlib.font_manager as fm
import matplotlib.pyplot as plt
 
mpl.rc('font', family="NanumBarunGothic")
plt.figure()
plt.title('안녕하세요')
 
cs




이제 이쁘게 잘 나온다.