Failures & Issues/problem-solution

Q. Matplotlib graph 한글깨짐현상 & (-) 부호 해결법 (IDE - Jupyter Notebook)

metamong 2022. 3. 27.

cf) 미국에 최적화된 IDE에 한글을 일일이 대입해줘야 한다.. ㅠㅠ 😐

 

(조건) IDE - Jupyter Notebook & Windows 환경 (Mac & Linux환경이나 colab에서 실습)

 

A.

↓↓↓↓ 아래 code 복붙 ↓↓↓↓

import matplotlib
from matplotlib import font_manager, rc

matplotlib.rcParams['axes.unicode_minus'] = False 

font_name = font_manager.FontProperties(fname="c:/Windows/Fonts/malgun.ttf").get_name()
rc('font', family=font_name)

 

-- 한글 글꼴 삽입 --

 

→ matplotlib module에서 font_manager와 rc method를 import한다

→ font_manager라는 method의 FontProperties라는 method에서 malgun.ttf라는 한글 글꼴을 지정해준다

(다른 한글글꼴도 가능하다. 나눔체도 가능했음. 각자 예쁜 글꼴 찾아서 그래프 예쁘게 꾸미시길 😉)

→ 그리고 matplotlib graph에 적용하기 위해 지정한 한글 글꼴을 rc method에 대입!

 

-- 마이너스 부호 삽입 --

 

→ matplotlib에 존재하는 rcParams라는 method가 있는데 'axes_unicode_minus'라는 attribute에 False값을 대입

 

Q. 왜 minus attribute에 False값을..? 마이너스 넣으려면 True 아닌가?

A. 적용한 한글 글꼴에 unicode minus 부호가 따로 지정되어 있지 않아서 그렇다. 이럴 경우 unicode minus값을 아예 False값으로 처리해주면 minus 자체를 문자로 인식해 graph에 matplotlib이 친절히 나타내준다.


예시> 한글 설치 전

 

→ "C:\-\-\-\-\-\matplotlib\backends\backend_agg.py:-: RuntimeWarning: Glyph - missing from current font. font.set_text(s, 0, flags=flags)" (현재 한글 font 없다고 에러 뜸)

위와 같은 에러 뜨면서;

 

- 전과 후 -


* 썸네일 출처) https://apps.shopify.com/easy-language-translate?locale=ko 

* 내용 출처) stackoverflow👍 https://stackoverflow.com/questions/43102564/matplotlib-negative-numbers-on-tick-labels-displayed-as-boxes

댓글