35. Python - Matplotlib 한글 설정 (환경 설정으로 고정), 마이너스 깨짐
Matplotlib에서 한글을 쓰려고 하면 깨지는데, 이것을 고치는 방법은 여러 개가 있습니다.
rcParam을 직접 지정해줄 수도 있고, FontProperties로 path를 가져오는 방법도 저번에 봤지만,
오늘은 주피터노트북이 실행될 때 바로 로드될 수 있도록 하는 방법을 가져왔습니다.
그래서 전역설정이나 매 번 설정하지 않아도 주피터노트북을 실행할 경우 자동으로 이제 한글이 됩니다.
import matplotlib.font_manager as fm
import matplotlib as mpl
font_list = fm.findSystemFonts(fontpaths=None, fontext='ttf')
print('설정 파일 위치', mpl.matplotlib_fname())
설치되어있는 폰트 리스트를 가져오고, 설정파일 위치를 알려줍니다.
따라서 폰트 설치는 아시죠?? ttf파일 받아서 공유폴더로 옮겨오셔서 설치하시면 됩니다!!
설정 파일 위치 /usr/local/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
font_list
['/usr/share/fonts/dejavu/DejaVuSerifCondensed-Bold.ttf', '/usr/share/fonts/un-core/UnGraphic.ttf', '/usr/share/fonts/un-core/UnGungseo.ttf', '/usr/share/fonts/lohit-telugu/Lohit-Telugu.ttf', '/usr/share/fonts/liberation/LiberationSans-BoldItalic.ttf', '/usr/share/fonts/un-core/UnBatang.ttf', '/usr/share/fonts/paktype/PakTypeTehreer.ttf', '/usr/share/fonts/vlgothic/VL-Gothic-Regular.ttf', '/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf', '/usr/share/fonts/dejavu/DejaVuSans.ttf', '/usr/share/fonts/un-core/UnDinaruBold.ttf', '/usr/share/fonts/opensymbol/opens___.ttf', '/usr/share/fonts/thai-scalable/Waree-BoldOblique.ttf', '/usr/share/fonts/un-core/UnDotumBold.ttf', '/usr/share/fonts/un-core/UnGraphicBold.ttf', '/usr/share/fonts/dejavu/DejaVuSansCondensed-Oblique.ttf', '/usr/share/fonts/lohit-devanagari/Lohit-Devanagari.ttf', '/usr/share/fonts/google-crosextra-caladea/Caladea-BoldItalic.ttf', '/usr/share/fonts/lohit-assamese/Lohit-Assamese.ttf', '/usr/share/fonts/JejuGothic/JejuGothic.ttf', '/usr/share/fonts/thai-scalable/Waree-Bold.ttf', '/usr/share/fonts/liberation/LiberationSerif-Regular.ttf', '/usr/share/fonts/madan/madan.ttf', '/usr/share/fonts/lohit-bengali/Lohit-Bengali.ttf', '/usr/share/fonts/lohit-gujarati/Lohit-Gujarati.ttf', '/usr/share/fonts/paktype/PakTypeNaqsh.ttf', '/usr/share/fonts/dejavu/DejaVuSansCondensed.ttf', '/usr/share/fonts/dejavu/DejaVuSerifCondensed-BoldItalic.ttf', '/usr/share/fonts/stix/STIXGeneralItalic.otf', '/usr/share/fonts/dejavu/DejaVuSerif-BoldItalic.ttf', '/usr/share/fonts/dejavu/DejaVuSerif-Italic.ttf', '/usr/share/fonts/un-core/UnBatangBold.ttf', '/usr/share/fonts/liberation/LiberationSerif-Italic.ttf', '/usr/share/fonts/sil-padauk/Padauk-Bold.ttf', '/usr/share/fonts/khmeros/KhmerOS.ttf', '/usr/share/fonts/un-core/UnDinaruLight.ttf', '/usr/share/fonts/google-crosextra-caladea/Caladea-Italic.ttf', '/usr/share/fonts/google-crosextra-carlito/Carlito-Regular.ttf', '/usr/share/fonts/liberation/LiberationSerif-BoldItalic.ttf', '/usr/share/fonts/dejavu/DejaVuSansCondensed-BoldOblique.ttf', '/usr/share/fonts/dejavu/DejaVuSansMono-BoldOblique.ttf', '/usr/share/fonts/khmeros/KhmerOS_sys.ttf', '/usr/share/fonts/dejavu/DejaVuSans-ExtraLight.ttf', '/usr/share/fonts/smc/Meera.ttf', '/usr/share/fonts/dejavu/DejaVuSans-BoldOblique.ttf', '/usr/share/fonts/google-crosextra-caladea/Caladea-Bold.ttf', '/usr/share/fonts/un-core/UnPilgi.ttf', '/usr/share/fonts/abyssinica/Abyssinica_SIL.ttf', '/usr/share/fonts/sil-padauk/Padauk.ttf', '/usr/share/fonts/lohit-tamil/Lohit-Tamil.ttf', '/usr/share/fonts/un-core/UnDinaru.ttf', '/usr/share/fonts/un-core/UnPilgiBold.ttf', '/usr/share/fonts/dejavu/DejaVuSansCondensed-Bold.ttf', '/usr/share/fonts/dejavu/DejaVuSansMono-Oblique.ttf', '/usr/share/fonts/dejavu/DejaVuSerif-Bold.ttf', '/usr/share/fonts/liberation/LiberationSans-Regular.ttf', '/usr/share/fonts/kurdit-unikurd-web/Unikuweb.ttf', '/usr/share/fonts/dejavu/DejaVuSerifCondensed.ttf', '/usr/share/fonts/dejavu/DejaVuSerif.ttf', '/usr/share/fonts/khmeros/KhmerOS_content.ttf', '/usr/share/fonts/liberation/LiberationMono-Bold.ttf', '/usr/share/fonts/lohit-oriya/Lohit-Oriya.ttf', '/usr/share/fonts/lohit-punjabi/Lohit-Punjabi.ttf', '/usr/share/fonts/google-crosextra-carlito/Carlito-Bold.ttf', '/usr/share/fonts/NanumGothic/NanumGothic.ttf', '/usr/share/fonts/thai-scalable/Waree-Oblique.ttf', '/usr/share/fonts/stix/STIXGeneral.otf', '/usr/share/fonts/liberation/LiberationMono-Italic.ttf', '/usr/share/fonts/dejavu/DejaVuSerifCondensed-Italic.ttf', '/usr/share/fonts/thai-scalable/Waree.ttf', '/usr/share/fonts/stix/STIXGeneralBol.otf', '/usr/share/fonts/google-crosextra-caladea/Caladea-Regular.ttf', '/usr/share/fonts/dejavu/DejaVuSans-Oblique.ttf', '/usr/share/fonts/stix/STIXGeneralBolIta.otf', '/usr/share/fonts/jomolhari/Jomolhari-alpha3c-0605331.ttf', '/usr/share/fonts/liberation/LiberationSerif-Bold.ttf', '/usr/share/fonts/dejavu/DejaVuSansMono-Bold.ttf', '/usr/share/fonts/un-core/UnDotum.ttf', '/usr/share/fonts/lohit-kannada/Lohit-Kannada.ttf', '/usr/share/fonts/google-crosextra-carlito/Carlito-BoldItalic.ttf', '/usr/share/fonts/dejavu/DejaVuSansMono.ttf', '/usr/share/fonts/lklug/lklug.ttf', '/usr/share/fonts/liberation/LiberationSans-Bold.ttf', '/usr/share/fonts/liberation/LiberationMono-Regular.ttf', '/usr/share/fonts/liberation/LiberationMono-BoldItalic.ttf', '/usr/share/fonts/liberation/LiberationSans-Italic.ttf', '/usr/share/fonts/google-crosextra-carlito/Carlito-Italic.ttf', '/usr/share/fonts/tibetan-machine-uni/TibMachUni-1.901b.ttf']
저렇게 font_list를 보면 저는 JejuGothic을 쓸 건데요, 가상머신 바탕화면에서
컴퓨터-> 파일시스템-> 그리고 아래 /usr/local/...쭉 가시면 됩니다.
그리고 matplotlibrc를 메모장으로 (gedit)여신 후에,
Ctrl+F로 font.famliy를 쳐주세요!
그리고 아래와 같이 바꿔줍니다.
font.family 옆의 # 주석을 풀어주시고, '이 부분'이라고 되어있는 곳에 폰트를 써줍니다.
저 같은 경우는 JejuGothic이라고 쓰면 되겠죠.
그후 저장하신 후, 주피터노트북을 껐다가 키시면 로드할때 저 폰트를 가져옵니다.
만약 적용이 안된다면, 폰트 이름을 잘못 쓴 것일 수 있습니다.
확인은 그래프를 직접 찍어보셔도 되고 이렇게 해도 됩니다.
import matplotlib.pyplot as plt
print(plt.rcParams['font.family'])
++
matplotlib.pyplot으로 그래프를 표현하다보면, 마이너스가 깨지는 현상이 있습니다.
이를 고치는 것 또한, 위에서 Ctrl+F 하여 axes.unicode.minus 를 찾으시고
주석 # 풀고, True를 False로 바꿔줍니다.
완성~~