본문 바로가기
파이썬(Python)

[파이썬(Python)] 데이터 시각화 한글폰트 설정하기 (matplotlib)

by Serendipity_ 2024. 11. 1.
반응형

파이썬 한글 폰트 시 깨지는 설정이 없으면 깨집니다.

 

한글폰트 미설정 시 깨짐현상

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, -4, 6, -5, 3]

plt.plot(x, y)
plt.title('한글 시 깨짐')

 

한글폰트 미설정 시 타이틀 깨짐

 

 

코드 한 줄 추가하여 설정을 수정해보겠습니다.

한글폰트 코드추가

import matplotlib
matplotlib.rcParams['font.family'] = 'Malgun Gothic' # Window 사용 시
# matplotlib.rcParams['font.family'] = 'AppleGothic' # Mac 사용 시

 

추가옵션(사이즈 및 마이너스 표기)

import matplotlib
matplotlib.rcParams['font.family'] = 'Malgun Gothic'
matplotlib.rcParams['font.size'] = 8 # 글자크기
matplotlib.rcParams['axes.unicode_minus'] = False # 한글 폰트 사용 시 마이너스 표기

 

글자 폰트를 원하는 폰트로 수정하고 싶을 경우에는 폰트 변경이 가능합니다.

import matplotlib.font_manager as fm
fm.fontManager.ttflist # 사용가능한 폰트 확인

 

저 같은 경우에는 'Gmarket Sans'용으로 한번 변경해보았습니다.

import matplotlib.font_manager as fm
[f.name for f in fm.fontManager.ttflist if 'gmarket' in f.name.lower()]

 

이런 식으로 코드를 입력하면 'gmarket'이 포함된 폰트를 불러와줍니다.

결과 )

['Gmarket Sans TTF', 'Gmarket Sans TTF', 'Gmarket Sans TTF']

 

한글폰트 최종설정

import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family'] = 'Gmarket Sans TTF'
matplotlib.rcParams['font.size'] = 8 # 글자크기
matplotlib.rcParams['axes.unicode_minus'] = False # 한글 폰트 사용 시 마이너스 표기

x = [1, 2, 3]
y = [2, -4, 6]
plt.plot(x, y);
plt.title('한글폰트 입력완료')
반응형

댓글