데이터분석

기초 03. matplotlib 한글 설정

책다니엘 2024. 3. 2. 05:19

https://bookdaniel.tistory.com/64

 

기초 01. 미니콘다 설치하기(Windows) & 환경설정

설치 1. 링크에 들어간다. (https://docs.anaconda.com/free/miniconda/index.html) Miniconda — Anaconda documentation These three commands quickly and quietly install the latest 64-bit version of the installer and then clean up after themselves. To

bookdaniel.tistory.com

https://bookdaniel.tistory.com/66

 

기초 02. Jupyter notebook 설치(Windows)

https://bookdaniel.tistory.com/64 기초 01. 미니콘다 설치하기(Windows) & 환경설정 설치 1. 링크에 들어간다. (https://docs.anaconda.com/free/miniconda/index.html) Miniconda — Anaconda documentation These three commands quickly and qui

bookdaniel.tistory.com

이후에 jupyter 창까지 띄워야 한다.

 

 

1. 새 노트북 파일을 하나 만들자.

 

 

2. 아래의 코드를 입력한다.

import matplotlib.pyplot as plt 
%matplotlib inline
plt.title("데이터사이언스")

 

해당 코드를 입력하면 아래처럼 빨간 음영표시된 에러가 표시되면서 그래프가 하나 뜬다. 그래프를 보면 이름 부분 폰트가 깨져서 나타난다. 이는 matplotlib패키지가 기본적으로 한글을 지원하지 않기 때문이다.

 

 

 

3. 아래 코드를 입력한다.

# mac의 경우

from matplotlib import font_manager
f_path = "/Library/Fonts/Arial Unicode.ttf" # tab
font_manager.FontProperties(fname=f_path).get_name()

# 'Arial Unicode MS'가 출력된다.

# Windows의 경우

from matplotlib import font_manager
f_path = "C:\Windows\Fonts\malgun.ttf" # tab
font_manager.FontProperties(fname=f_path).get_name()

# 'Malgun Gothic'이 출력된다.

 

 

4. 새로운 코드 창을 하나 열고(코드창 바깥의 파란 영역을 클릭하고 b, 지울 때는 d 두 번) 아래의 코드를 입력한다.

from matplotlib import rc
rc('font', family = "Malgun Gothic")

 

 

5. 그러면 기본 폰트가 변경되고, 그래프 이름에 한글이 정상적으로 출력된다. 

 

쨘!

 

결론

앞으로 jupyter notebook 가상환경에서 무언가를 실행할 때는, 항상 이렇게 코드를 넣어줘야 한다.

# Windows 기준

import matplotlib.pyplot as plt 
%matplotlib inline

from matplotlib import rc
rc('font', family = "Malgun Gothic")
plt.title("여기에 제목을 입력하세요")