반응형
문제
Colab에서 python을 통해 그래프(plot)를 그릴 경우, 한글깨짐 현상이 나타날 수 있습니다.
해결
#나눔 폰트 설치
!sudo apt-get install -y fonts-nanum
!sudo fc-cache -fv
!rm ~/.cache/matplotlib -rf
#matplotlib font 설정
import matplotlib .pyplot as plt
plt.rc('font', family='NanumGothic')
위의 코드실행 후 다시 그래프를 그릴 경우 아래와 같이 한글이 잘 나타납니다.
혹시 깨짐현상이 다시 발생하는 경우 런타임 다시시작 클릭 후 실행하면 한글이 나타납니다
추가-다른글꼴사용
나눔고딕이 글씨체가 마음에 들지 않다면, 글꼴을 설정할 수 있습니다.
아래 코드를 입력하면 현재 코랩환경상에서 사용할 수 있는 font 목록을 불러올 수 있습니다.
import matplotlib.font_manager as fm
sys_font=fm.findSystemFonts()
print(f"sys_font number: {len(sys_font)}")
sys_font
font 리스트에서 마음에 드는 글꼴을 선택하여 그래프를 그리면 됩니다.
#원하는 글꼴 설정
plt.rc('font', family='NanumMyeongjo')
df_merge[df_merge['name']=='taegu'].groupby('datetime')['title'].count().plot()
plt.title('태구 Trend',fontsize=15)
plt.show()
반응형
'언어 > Python' 카테고리의 다른 글
[Python] TypeError: a bytes-like object is required, not 'str' (0) | 2022.09.21 |
---|---|
[Python] SMTPAuthenticationError (0) | 2022.07.05 |
[Python] TypeError: pos() missing 1 required positional argument: 'phrase' (1) | 2021.10.26 |
[Python] 'tqdm_notebook' object has no attribute 'delay' (0) | 2021.10.25 |
[python] 공공데이터 포털 API사용 (0) | 2021.08.10 |