데이터를 가지고 여러가지 시각화를 하다보면 한글 폰트가 깨지는 경우가 종종 있다.
하지만 매번 해결하는 법을 까먹는 나...
한글 폰트 깨짐을 해결하는 방법은 여러가지있지만 나는 주로 아래의 코드로 해결한다.
# 그래프 시각화 옵션
%matplotlib inline
import seaborn as sns
from matplotlib import pyplot as plt
import matplotlib as mpl
import matplotlib.font_manager as fm
import warnings
warnings.filterwarnings("ignore")
# unicode minus를 사용하지 않기 위한 설정 (minus 깨짐현상 방지)
plt.rcParams['axes.unicode_minus'] = False
# 폰트 설치
!sudo apt-get install -y fonts-nanum
!sudo fc-cache -fv
!rm ~/.cache/matplotlib -rf
plt.rc('font', family='NanumGothic')
#그래프의 한글을 더욱 선명하게 출력
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('retina')
위와 같이 코드를 작성해주면 한글폰트도 제대로 나온다
(만약 제대로 안나오면 런타임 다시 시작해주면 될거다!!!)
요약
# 설정 변환을 위해서 matplotlib만 import
import matplotlib as mpl
# Windows
# mpl.rc("font", family='Malgun Gothic')
# MacOS
mpl.rc("font", family='AppleGothic')
# 이번에는 axes 설정을 바꿔줍니다
mpl.rc('axes', unicode_minus=False)
# 폰트 변환
mpl.rc("font", family='Malgun Gothic') # Windows
# 마이너스 사인 수정
mpl.rc('axes', unicode_minus=False)
'Python' 카테고리의 다른 글
Section 5 과제 수행과정 (1) | 2021.05.06 |
---|---|
[Error 해결] ValueError: Iterable over raw text documents expected, string object received. (0) | 2021.05.04 |
[Error해결] ValueError: No gradients provided for any variable (0) | 2021.04.23 |
[Error해결]ResourceExhaustedError: OOM (0) | 2021.04.23 |
[Tensorflow] AttributeError: module 'tensorflow' has no attribute 'placeholder' 오류 해결하기 (0) | 2021.04.08 |
댓글