• 카테고리

    질문 & 답변
  • 세부 분야

    데이터 분석

  • 해결 여부

    미해결

한글폰트가 깨집니다

22.07.14 11:26 작성 조회수 912

2

def get_font_family():
    """
    시스템 환경에 따른 기본 폰트명을 반환하는 함수
    """
    import platform
    system_name = platform.system()
    # colab 사용자는 system_name이 'Linux'로 확인

    if system_name == "Darwin" :
        font_family = "AppleGothic"
    elif system_name == "Windows":
        font_family = "Malgun Gothic"
    else:
        !apt-get install fonts-nanum -qq  > /dev/null
        !fc-cache -fv

        import matplotlib as mpl
        mpl.font_manager._rebuild()
        findfont = mpl.font_manager.fontManager.findfont
        mpl.font_manager.findfont = findfont
        mpl.backends.backend_agg.findfont = findfont
        
        font_family = "NanumBarunGothic"
        
    return font_family

이렇게 설정하고 get_font_family()하면 'Malgun Gothic' 제대로 나오고

# 시각화를 위한 폰트설정
import matplotlib.pyplot as plt

# 위에서 만든 함수를 통해 시스템 폰트를 불러와서 font_family 라는 변수에 할당
font_family = get_font_family()

# 폰트설정
plt.rc("font", family = "font_family")

# 마이너스폰트 설정
plt.rc("axes", unicode_minus = False)

그래프 스타일 설정
plt.style.use("ggplot")

 

이걸 돌렸는데도 

# 한글폰트 확인
pd.Series([1, -3]).plot(title="한글폰트")

하면 깨져서 나옵니다.

 

C:\Users\August\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:240: RuntimeWarning: Glyph 54620 missing from current font.
  font.set_text(s, 0.0, flags=flags)

어떻게 하면 좋을까요?

답변 2

·

답변을 작성해보세요.

1

안녕하세요.

 

아마 윈도우 기본폰트에 맑은 고딕이 없는 것으로 보여집니다.

아래 명령어를 conda prompt 를 열어 설치해 주세요!

pip install koreanize-matplotlib

 

그리고 주피터 노트북을 열어 아래의 코드를 실행해 보세요.

koreanize_matplotlib 은 설치하고 불러오는 것만으로도 한글폰트 사용이 가능합니다.

 

import koreanize_matplotlib

# 그래프에 retina display 적용
%config InlineBackend.figure_format = 'retina'

pd.Series([1, 3, 5, -7, 9]).plot(title="한글")

 

hmin님의 프로필

hmin

질문자

2022.07.15

해결 됐습니다. 감사합니다!

0

브루브루님의 프로필

브루브루

2022.07.22

For 아이패드 사용자

저는 출퇴근길에 아이패드에서 보다보니 환경이 조금 달랐습니다.

비슷한 경우가 있었는데 아래처럼 아이패드 기본 폰트로 설정하니 됐어요. 

 

 

def get_font_family():

    """

    시스템 환경에 따른 기본 폰트명을 반환하는 함수

    """

    import platform

    system_name = platform.system()

    # colab 사용자는 system_name이 'Linux'로 확인

 

    if system_name == "Darwin" :

        # font_family = "AppleGothic"

        font_family = "Apple SD Gothic Neo"

        

    elif system_name == "Windows":

        font_family = "Malgun Gothic"

    else:

        # Linux

        # colab에서는 runtime을 <꼭> 재시작 해야합니다.

        # 런타임을 재시작 하지 않고 폰트 설치를 하면 기본 설정 폰트가 로드되어 한글이 깨집니다.

        !apt-get update -qq

        !apt-get install fonts-nanum -qq  > /dev/null

 

        import matplotlib.font_manager as fm

 

        fontpath = '/usr/share/fonts/truetype/nanum/NanumBarunGothic.ttf'

        font = fm.FontProperties(fname=fontpath, size=9)

        fm._rebuild()

        font_family = "NanumBarunGothic"

    return font_family