|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Ubuntu Matplotlib 한글 인식 방법" |
| 4 | +date: 2020-08-04 |
| 5 | +category: etc |
| 6 | +tags: ubuntu, matplotlib, korean |
| 7 | +author: Diominor, 백승열 |
| 8 | +comments: true |
| 9 | +--- |
| 10 | + |
| 11 | +참조 : [http://taewan.kim/post/matplotlib_hangul](http://taewan.kim/post/matplotlib_hangul/) |
| 12 | + |
| 13 | +방법 중에서도 anaconda가 설치 되어 있을 때 |
| 14 | + |
| 15 | +1. 우분투 한글 폰트 설치 |
| 16 | + ``` |
| 17 | + $ sudo apt-get install -y fonts-nanum fonts-nanum-coding fonts-nanum-extra |
| 18 | + ``` |
| 19 | + 재부팅 하면 폰트가 인식 되지만 재부팅 하기 싫을 경우 아래 코드 실행 |
| 20 | + ``` |
| 21 | + $ fc-cache -fv |
| 22 | + ``` |
| 23 | + |
| 24 | +2. 폰트 anaconda의 matplotlib에 옮기기 |
| 25 | +
|
| 26 | + 시스템에 설치된 나눔 글꼴을 아나콘다의 matplotlib패키지 안의 fonts/ttf 파일로 옮겨줍니다. |
| 27 | + ``` |
| 28 | + $ sudo cp /usr/share/fonts/truetype/nanum/Nanum* ~/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf/ |
| 29 | + ``` |
| 30 | + (anaconda, python의 버전 및 개인 설정에 따라 '.../fonts/ttf/'의 경로는 바뀔 수 있으니 확인하고 입력해주세요.) |
| 31 | + |
| 32 | +2. Matplotlib 폰트 정보 확인 |
| 33 | +
|
| 34 | + matplotlib은 사용할 font 정보를 fontlist-v310.json에 관리합니다. |
| 35 | + |
| 36 | + (이름이 'fontlist-v310.json'와 정확히 일치하지 않을 수도 있습니다.) |
| 37 | + ``` |
| 38 | + $ ls -al ~/.cache/matplotlib/ |
| 39 | + 합계 104 |
| 40 | + drwxr-xr-x 3 backgom2357 backgom2357 4096 6월 17 08:05 . |
| 41 | + drwx------ 27 backgom2357 backgom2357 4096 6월 16 13:59 .. |
| 42 | + -rw-r--r-- 1 backgom2357 backgom2357 92708 6월 17 08:05 fontlist-v310.json |
| 43 | + drwxr-xr-x 2 backgom2357 backgom2357 4096 6월 2 11:44 tex.cache |
| 44 | + ``` |
| 45 | +
|
| 46 | +3. json 수정 |
| 47 | +
|
| 48 | + vim을 이용해 json을 열어 font 정보를 추가합니다. |
| 49 | + ``` |
| 50 | + $ vim ~/.cache/matplotlib/fontlist-v310.json |
| 51 | + ``` |
| 52 | + |
| 53 | + - fontlist-v310.json |
| 54 | + |
| 55 | + "ttflist": \[...\] 폰트 정보를 양식에 따라 리스트에 추가합니다. |
| 56 | + ``` |
| 57 | + { |
| 58 | + "_version": 310, |
| 59 | + "_FontManager__default_weight": "normal", |
| 60 | + "default_size": null, |
| 61 | + "defaultFamily": { |
| 62 | + "ttf": "DejaVu Sans", |
| 63 | + "afm": "Helvetica" |
| 64 | + }, |
| 65 | + "ttflist": [ |
| 66 | + |
| 67 | + { |
| 68 | + "fname": "fonts/ttf/[폰트 이름].ttf", |
| 69 | + "name": "[폰트 이름]", |
| 70 | + "style": "normal", |
| 71 | + "variant": "normal", |
| 72 | + "weight": 400, |
| 73 | + "stretch": "normal", |
| 74 | + "size": "scalable", |
| 75 | + "__class__": "FontEntry" |
| 76 | +
|
| 77 | + }, |
| 78 | + { |
| 79 | + "fname": "fonts/ttf/STIXGeneralItalic.ttf", |
| 80 | + "name": "STIXGeneral", |
| 81 | + "style": "normal", |
| 82 | + "variant": "normal", |
| 83 | + "weight": 400, |
| 84 | + "stretch": "normal", |
| 85 | + "size": "scalable", |
| 86 | + "__class__": "FontEntry" |
| 87 | + }, |
| 88 | + ... |
| 89 | + ] |
| 90 | + ... |
| 91 | +
|
| 92 | + ``` |
| 93 | + (\[폰트이름\] 예 : NanumGothic) |
| 94 | + |
| 95 | +
|
| 96 | +4. 확인하기 |
| 97 | + |
| 98 | + console, jupyter notebook 등을 통해 확인합니다. |
| 99 | + ``` |
| 100 | + import matplotlib |
| 101 | + import matplotlib.font_manager as fm |
| 102 | + font_list = fm.fontManager.ttflist |
| 103 | + [f.name for f in font_list if 'Nanum' in f.name] |
| 104 | + # ["[폰트 이름]"] |
| 105 | + ``` |
| 106 | + |
| 107 | +5. 사용하기 |
| 108 | +
|
| 109 | + matplotlib에 원하는 폰트를 설정하는 코드는 여러 가지가 있지만 그 중 하나를 소개합니다. |
| 110 | + ``` |
| 111 | + import matplotlib.pyplot as plt |
| 112 | + from matplotlib import rc |
| 113 | + rc('font', family="[폰트 이름]") |
0 commit comments