인프런 커뮤니티 질문&답변

amethis님의 프로필 이미지
amethis

작성한 질문수

파이썬 증권 데이터 수집과 분석으로 신호와 소음 찾기

데이터 시각화 도구 소개와 한글폰트 설정 에러

작성

·

2.1K

1

데이터 시각화 도구 소개와 한글폰트 설정 에러

위에 강좌를 실습 중에 에러가 발생합니다.

우분투, 주피터노트북 사용, 나눔바른고딕 폰트 있습니다.

도와주세요!!!!

 

1. 우분투에 나눔바른고딕 폰트 있음: 제가 실습하는 환경

(base) root@gd-virtual-machine:/usr/share/fonts/truetype/nanum# lsb_release -d

Description:    Ubuntu 20.04.4 LTS

(base) root@gd-virtual-machine:/usr/share/fonts/truetype/nanum#

(base) root@gd-virtual-machine:/usr/share/fonts/truetype/nanum#

(base) root@gd-virtual-machine:/usr/share/fonts/truetype/nanum# ls NanumBarunGothic.ttf

NanumBarunGothic.ttf

(base) root@gd-virtual-machine:/usr/share/fonts/truetype/nanum#

 

2. 에러 발생(강의 코드)

def get_font_family():

    import platform

    system_name = platform.system()

    

    if system_name == "Darwin" :

        font_family = "AppleGothic"

    elif system_name == "Windows":

        font_family = "Malgun Gothic"

    else:

        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

 

AttributeError                            Traceback (most recent call last)

/tmp/ipykernel_1662/385091406.py in <module>

----> 1 get_font_family()

 

/tmp/ipykernel_1662/3056356644.py in get_font_family()

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

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

---> 13         fm._rebuild()

     14         font_family = "NanumBarunGothic"

     15     return font_family

 

AttributeError: module 'matplotlib.font_manager' has no attribute '_rebuild'

 

3. 에러 발생(수업자료 소스 코드)

def get_font_family():

    """

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

    """

    import platform

    system_name = platform.system()

 

    if system_name == "Darwin" :

        font_family = "AppleGothic"

    elif system_name == "Windows":

        font_family = "Malgun Gothic"

    else:

        # Linux(colab)

        !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

 

/usr/share/fonts: caching, new cache contents: 0 fonts, 6 dirs

/usr/share/fonts/X11: caching, new cache contents: 0 fonts, 4 dirs

/usr/share/fonts/X11/Type1: caching, new cache contents: 8 fonts, 0 dirs

/usr/share/fonts/X11/encodings: caching, new cache contents: 0 fonts, 1 dirs

/usr/share/fonts/X11/encodings/large: caching, new cache contents: 0 fonts, 0 dirs

/usr/share/fonts/X11/misc: caching, new cache contents: 89 fonts, 0 dirs

/usr/share/fonts/X11/util: caching, new cache contents: 0 fonts, 0 dirs

/usr/share/fonts/cMap: caching, new cache contents: 0 fonts, 0 dirs

/usr/share/fonts/cmap: caching, new cache contents: 0 fonts, 5 dirs

/usr/share/fonts/cmap/adobe-cns1: caching, new cache contents: 0 fonts, 0 dirs

/usr/share/fonts/cmap/adobe-gb1: caching, new cache contents: 0 fonts, 0 dirs

/usr/share/fonts/cmap/adobe-japan1: caching, new cache contents: 0 fonts, 0 dirs

/usr/share/fonts/cmap/adobe-japan2: caching, new cache contents: 0 fonts, 0 dirs

/usr/share/fonts/cmap/adobe-korea1: caching, new cache contents: 0 fonts, 0 dirs

/usr/share/fonts/opentype: caching, new cache contents: 0 fonts, 3 dirs

/usr/share/fonts/opentype/malayalam: caching, new cache contents: 7 fonts, 0 dirs

/usr/share/fonts/opentype/noto: caching, new cache contents: 28 fonts, 0 dirs

/usr/share/fonts/opentype/urw-base35: caching, new cache contents: 35 fonts, 0 dirs

/usr/share/fonts/truetype: caching, new cache contents: 0 fonts, 50 dirs

/usr/share/fonts/truetype/Gargi: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/Gubbi: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/Nakula: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/Navilu: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/Sahadeva: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/Sarai: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/abyssinica: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/arphic: caching, new cache contents: 0 fonts, 0 dirs

/usr/share/fonts/truetype/dejavu: caching, new cache contents: 6 fonts, 0 dirs

/usr/share/fonts/truetype/droid: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/fonts-beng-extra: caching, new cache contents: 6 fonts, 0 dirs

/usr/share/fonts/truetype/fonts-deva-extra: caching, new cache contents: 3 fonts, 0 dirs

/usr/share/fonts/truetype/fonts-gujr-extra: caching, new cache contents: 5 fonts, 0 dirs

/usr/share/fonts/truetype/fonts-guru-extra: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/fonts-kalapi: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/fonts-orya-extra: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/fonts-telu-extra: caching, new cache contents: 2 fonts, 0 dirs

/usr/share/fonts/truetype/fonts-yrsa-rasa: caching, new cache contents: 10 fonts, 0 dirs

/usr/share/fonts/truetype/freefont: caching, new cache contents: 12 fonts, 0 dirs

/usr/share/fonts/truetype/kacst: caching, new cache contents: 15 fonts, 0 dirs

/usr/share/fonts/truetype/kacst-one: caching, new cache contents: 2 fonts, 0 dirs

/usr/share/fonts/truetype/lao: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/liberation: caching, new cache contents: 16 fonts, 0 dirs

/usr/share/fonts/truetype/liberation2: caching, new cache contents: 12 fonts, 0 dirs

/usr/share/fonts/truetype/lohit-assamese: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/lohit-bengali: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/lohit-devanagari: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/lohit-gujarati: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/lohit-kannada: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/lohit-malayalam: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/lohit-oriya: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/lohit-punjabi: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/lohit-tamil: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/lohit-tamil-classical: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/lohit-telugu: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/lyx: caching, new cache contents: 11 fonts, 0 dirs

/usr/share/fonts/truetype/malayalam: caching, new cache contents: 10 fonts, 0 dirs

/usr/share/fonts/truetype/nanum: caching, new cache contents: 31 fonts, 0 dirs

/usr/share/fonts/truetype/noto: caching, new cache contents: 2 fonts, 0 dirs

/usr/share/fonts/truetype/openoffice: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/padauk: caching, new cache contents: 4 fonts, 0 dirs

/usr/share/fonts/truetype/pagul: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/samyak: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/samyak-fonts: caching, new cache contents: 3 fonts, 0 dirs

/usr/share/fonts/truetype/sinhala: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/tibetan-machine: caching, new cache contents: 1 fonts, 0 dirs

/usr/share/fonts/truetype/tlwg: caching, new cache contents: 58 fonts, 0 dirs

/usr/share/fonts/truetype/ttf-bitstream-vera: caching, new cache contents: 10 fonts, 0 dirs

/usr/share/fonts/truetype/ttf-khmeros-core: caching, new cache contents: 2 fonts, 0 dirs

/usr/share/fonts/truetype/ubuntu: caching, new cache contents: 14 fonts, 0 dirs

/usr/share/fonts/type1: caching, new cache contents: 0 fonts, 1 dirs

/usr/share/fonts/type1/urw-base35: caching, new cache contents: 35 fonts, 0 dirs

/root/anaconda3/fonts: skipping, no such directory

/root/.local/share/fonts: skipping, no such directory

/root/.fonts: skipping, no such directory

/usr/share/fonts/X11: skipping, looped directory detected

/usr/share/fonts/cMap: skipping, looped directory detected

/usr/share/fonts/cmap: skipping, looped directory detected

/usr/share/fonts/opentype: skipping, looped directory detected

/usr/share/fonts/truetype: skipping, looped directory detected

/usr/share/fonts/type1: skipping, looped directory detected

/usr/share/fonts/X11/Type1: skipping, looped directory detected

/usr/share/fonts/X11/encodings: skipping, looped directory detected

/usr/share/fonts/X11/misc: skipping, looped directory detected

/usr/share/fonts/X11/util: skipping, looped directory detected

/usr/share/fonts/cmap/adobe-cns1: skipping, looped directory detected

/usr/share/fonts/cmap/adobe-gb1: skipping, looped directory detected

/usr/share/fonts/cmap/adobe-japan1: skipping, looped directory detected

/usr/share/fonts/cmap/adobe-japan2: skipping, looped directory detected

/usr/share/fonts/cmap/adobe-korea1: skipping, looped directory detected

/usr/share/fonts/opentype/malayalam: skipping, looped directory detected

/usr/share/fonts/opentype/noto: skipping, looped directory detected

/usr/share/fonts/opentype/urw-base35: skipping, looped directory detected

/usr/share/fonts/truetype/Gargi: skipping, looped directory detected

/usr/share/fonts/truetype/Gubbi: skipping, looped directory detected

/usr/share/fonts/truetype/Nakula: skipping, looped directory detected

/usr/share/fonts/truetype/Navilu: skipping, looped directory detected

/usr/share/fonts/truetype/Sahadeva: skipping, looped directory detected

/usr/share/fonts/truetype/Sarai: skipping, looped directory detected

/usr/share/fonts/truetype/abyssinica: skipping, looped directory detected

/usr/share/fonts/truetype/arphic: skipping, looped directory detected

/usr/share/fonts/truetype/dejavu: skipping, looped directory detected

/usr/share/fonts/truetype/droid: skipping, looped directory detected

/usr/share/fonts/truetype/fonts-beng-extra: skipping, looped directory detected

/usr/share/fonts/truetype/fonts-deva-extra: skipping, looped directory detected

/usr/share/fonts/truetype/fonts-gujr-extra: skipping, looped directory detected

/usr/share/fonts/truetype/fonts-guru-extra: skipping, looped directory detected

/usr/share/fonts/truetype/fonts-kalapi: skipping, looped directory detected

/usr/share/fonts/truetype/fonts-orya-extra: skipping, looped directory detected

/usr/share/fonts/truetype/fonts-telu-extra: skipping, looped directory detected

/usr/share/fonts/truetype/fonts-yrsa-rasa: skipping, looped directory detected

/usr/share/fonts/truetype/freefont: skipping, looped directory detected

/usr/share/fonts/truetype/kacst: skipping, looped directory detected

/usr/share/fonts/truetype/kacst-one: skipping, looped directory detected

/usr/share/fonts/truetype/lao: skipping, looped directory detected

/usr/share/fonts/truetype/liberation: skipping, looped directory detected

/usr/share/fonts/truetype/liberation2: skipping, looped directory detected

/usr/share/fonts/truetype/lohit-assamese: skipping, looped directory detected

/usr/share/fonts/truetype/lohit-bengali: skipping, looped directory detected

/usr/share/fonts/truetype/lohit-devanagari: skipping, looped directory detected

/usr/share/fonts/truetype/lohit-gujarati: skipping, looped directory detected

/usr/share/fonts/truetype/lohit-kannada: skipping, looped directory detected

/usr/share/fonts/truetype/lohit-malayalam: skipping, looped directory detected

/usr/share/fonts/truetype/lohit-oriya: skipping, looped directory detected

/usr/share/fonts/truetype/lohit-punjabi: skipping, looped directory detected

/usr/share/fonts/truetype/lohit-tamil: skipping, looped directory detected

/usr/share/fonts/truetype/lohit-tamil-classical: skipping, looped directory detected

/usr/share/fonts/truetype/lohit-telugu: skipping, looped directory detected

/usr/share/fonts/truetype/lyx: skipping, looped directory detected

/usr/share/fonts/truetype/malayalam: skipping, looped directory detected

/usr/share/fonts/truetype/nanum: skipping, looped directory detected

/usr/share/fonts/truetype/noto: skipping, looped directory detected

/usr/share/fonts/truetype/openoffice: skipping, looped directory detected

/usr/share/fonts/truetype/padauk: skipping, looped directory detected

/usr/share/fonts/truetype/pagul: skipping, looped directory detected

/usr/share/fonts/truetype/samyak: skipping, looped directory detected

/usr/share/fonts/truetype/samyak-fonts: skipping, looped directory detected

/usr/share/fonts/truetype/sinhala: skipping, looped directory detected

/usr/share/fonts/truetype/tibetan-machine: skipping, looped directory detected

/usr/share/fonts/truetype/tlwg: skipping, looped directory detected

/usr/share/fonts/truetype/ttf-bitstream-vera: skipping, looped directory detected

/usr/share/fonts/truetype/ttf-khmeros-core: skipping, looped directory detected

/usr/share/fonts/truetype/ubuntu: skipping, looped directory detected

/usr/share/fonts/type1/urw-base35: skipping, looped directory detected

/usr/share/fonts/X11/encodings/large: skipping, looped directory detected

/root/anaconda3/var/cache/fontconfig: cleaning cache directory

/root/.cache/fontconfig: not cleaning non-existent cache directory

/root/.fontconfig: not cleaning non-existent cache directory

fc-cache: succeeded

---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

/tmp/ipykernel_1662/385091406.py in <module>

----> 1 get_font_family()

 

/tmp/ipykernel_1662/3296694249.py in get_font_family()

     16 

     17         import matplotlib as mpl

---> 18         mpl.font_manager._rebuild()

     19         findfont = mpl.font_manager.fontManager.findfont

     20         mpl.font_manager.findfont = findfont

 

AttributeError: module 'matplotlib.font_manager' has no attribute '_rebuild'

    

답변 1

1

박조은님의 프로필 이미지
박조은
지식공유자

안녕하세요!

강의 제작 당시에는 폰트를 설치해서 사용하는 것으로 제작했는데  아래의 라이브러리가 공개되어 아래 라이브러리를 사용해 보는것을 추천합니다.

설치와 import 만으로도 한글폰트를 사용하실 수 있습니다.

github : https://github.com/ychoi-kr/koreanize-matplotlib

설치

pip install koreanize-matplotlib

또는

python setup.py install

import matplotlib.pyplot as plt
import koreanize_matplotlib

plt.plot([-1, 0, 1, 2])
plt.xlabel('간단한 그래프')
plt.show()
amethis님의 프로필 이미지
amethis
질문자

해결되었습니다. 너무 너무 감사합니다...!!!!

amethis님의 프로필 이미지
amethis

작성한 질문수

질문하기