[ํ์ด์ฌ] ์ฐ๋ถํฌ์์ ํ๊ธ ํฐํธ ์ค์นํ๊ณ matplotlib์ ์ฌ์ฉํ๊ธฐ
์๋ณธ ๊ฒ์๊ธ: https://velog.io/@euisuk-chung/ํ์ด์ฌ-์ฐ๋ถํฌ์์-ํ๊ธ-ํฐํธ-์ค์นํ๊ณ -matplotlib์-์ฌ์ฉํ๊ธฐ
์๋ ํ์ธ์๐ ์ค๋์ ์ฐ๋ถํฌ์์ ํ๊ธ ํฐํธ ์ค์นํ๊ณ matplotlib์์ ํด๋น ํฐํธ๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์ ๋ฆฌํด๋ณด๊ฒ ์ต๋๋ค!
ํ์ด์ฌ์ ํ์ฉํ๋ค๋ณด๋ฉด ์๊ฐํ๋ ํ์ ์์ญ์
๋๋ค. ๊ทธ๋ฌ๋ Linux ํ๊ฒฝ์์๋ ๊ธฐ๋ณธ์ ์ผ๋ก ํ๊ธ ํฐํธ๊ฐ ์ค์น๋์ด ์์ง ์์ ํ๊ธ์ด ์ ๋๋ก ํ์๋์ง ์๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค. ๋ณธ ๊ฒ์๊ธ์ ์ ๋ชฉ์ฒ๋ผ ๋ค๋ชจ๋ค๋ชจ ๋น
์ ๋ง์ ๊ฒ์ฒ๋ผ ๊นจ์ ธ์ ๋์ค๊ฒ ๋ฉ๋๋ค ๐ฑ
์ด๋ ๊ฒ ์ถ๋ ฅ๋๋ ์ฃผ๋ ์ด์ ๋ ์ด์์ฒด์ ์ ๊ธฐ๋ณธ ํฐํธ ์ธํธ์ ํ๊ธ ํฐํธ๊ฐ ํฌํจ๋์ง ์๊ธฐ ๋๋ฌธ์ ๋๋ค. ๋ฐ๋ผ์ ๋ณ๋๋ก ํ๊ธ ํฐํธ๋ฅผ ์ค์นํด์ผ ํฉ๋๋ค. ์ด๋ฒ ๊ธ์์๋ Ubuntu ํ๊ฒฝ์์ ๋๋ ํฐํธ๋ฅผ ์ฌ์ฉํ์ฌ Matplotlib์ ์ ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋จ๊ณ๋ณ๋ก ์์ธํ ์ ๋ฆฌํ๊ฒ ์ต๋๋ค.
์๋ ๋ฐฉ๋ฒ์ ๋ฐ๋ฅด๋ฉด 5๋ถ ๋ด๋ก ํฐํธ๋ฅผ ์ค์ ํ ์ ์์ต๋๋ค.
-
๋๋ ํฐํธ ์ค์น
์ฐ์ , ๋๋ ํฐํธ๋ฅผ ์ค์นํฉ๋๋ค. ํฐ๋ฏธ๋์์ ๋ค์ ๋ช ๋ น์ด๋ฅผ ์คํํฉ๋๋ค.
1
sudo apt-get install fonts-nanum*
-
ํฐํธ ์บ์ ์ญ์
ํฐํธ ์บ์๋ฅผ ๊ฐฑ์ ํ๊ธฐ ์ํด ์บ์๋ฅผ ์ญ์ ํฉ๋๋ค.
1
fc-cache -fv
-
ํฐํธ ๋ฐฐํฌ
Matplotlib์ ํฐํธ ๋๋ ํฐ๋ฆฌ์ ๋๋ ํฐํธ๋ฅผ ๋ณต์ฌํด์ผ ํฉ๋๋ค. ๋จผ์ , Matplotlib ์ค์ ๋๋ ํฐ๋ฆฌ๊ฐ ์ด๋ ์๋์ง ํ์ธํฉ๋๋ค.
1
2
import matplotlib
print(matplotlib.__file__)
์ ๋ช ๋ น์ด๋ฅผ ์คํํ๋ฉด Matplotlib์ ๊ฒฝ๋ก๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค. ์๋ฅผ ๋ค์ด:
1
/usr/anaconda3/lib/python3.7/site-packages/matplotlib/__init__.py
์ด ๊ฒฝ๋ก๋ฅผ ๊ธฐ์ค์ผ๋ก, mpl-data/fonts/ttf
๋๋ ํฐ๋ฆฌ์ ํฐํธ๋ฅผ ๋ณต์ฌํฉ๋๋ค.
์๋ฅผ ๋ค์ด Anaconda ํ๊ฒฝ์ด๋ผ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋ณต์ฌํฉ๋๋ค.
1
cp /usr/share/fonts/truetype/nanum/Nanum* /usr/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf/
-
Matplotlib ํฐํธ ์บ์ ์ญ์
Matplotlib์ ํฐํธ ์บ์๋ฅผ ์ญ์ ํ์ฌ ์๋ก์ด ํฐํธ๋ฅผ ์ธ์ํ๋๋ก ํฉ๋๋ค.
1
rm -rf ~/.cache/matplotlib/*
-
ํฐํธ ์ ์ฉ
ํ์ด์ฌ ํน์ ์ฃผํผํฐ ๋ ธํธ๋ถ์ ์ฌ์คํํ์ฌ ํฐํธ๋ฅผ ๋ก๋ํฉ๋๋ค. ํฐํธ๊ฐ ์ค์น๋์๋์ง ํ์ธํ๋ ๋ช ๋ น์ด๋ฅผ ์คํํฉ๋๋ค.
1
2
import matplotlib.font_manager
print([f.fname for f in matplotlib.font_manager.fontManager.ttflist])
์ ๋ช
๋ น์ด๋ฅผ ์คํํ๋ฉด ์ค์น๋ ํฐํธ ๋ฆฌ์คํธ๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค. Nanum
์ผ๋ก ์์ํ๋ ํฐํธ๋ค์ด ๋ณด์ด๋ฉด ์ ๋๋ก ์ค์น๋ ๊ฒ์
๋๋ค.
ํฐํธ ์ด๋ฆ์ ํ์ธํ๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
1
2
3
import matplotlib.font_manager
font_list = matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
print([matplotlib.font_manager.FontProperties(fname=font).get_name() for font in font_list])
์ ๋ช ๋ น์ด๋ฅผ ์คํํ๋ฉด ํฐํธ ์ด๋ฆ๋ค์ด ์ถ๋ ฅ๋ฉ๋๋ค. ์๋ฅผ ๋ค์ด:
1
['Nanum Brush Script', 'NanumBarunGothic', 'NanumSquareRound', ...]
์ด์ ํฐํธ๋ฅผ Matplotlib์์ ์ ์ฉํ ์ ์์ต๋๋ค.
-
์์ ์ฝ๋ ์คํ
๋ค์ ์์ ์ฝ๋๋ฅผ ์ฌ์ฉํ์ฌ ๋๋ ํฐํธ๋ฅผ ์ค์ ํ๊ณ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ ค๋ด ๋๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import matplotlib.pyplot as plt
import matplotlib as mpl
# ํฐํธ ์ค์
plt.rcParams["font.family"] = 'NanumGothic'
mpl.rcParams['axes.unicode_minus'] = False # ๋ง์ด๋์ค ํฐํธ ๊นจ์ง ๋ฐฉ์ง
# ์์ ๊ทธ๋ํ
plt.figure(figsize=(8, 6))
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.title('์์ ๊ทธ๋ํ')
plt.xlabel('X ์ถ')
plt.ylabel('Y ์ถ')
plt.show()
์ด์ ํ๊ธ์ด ์ ๋๋ก ์ถ๋ ฅ๋๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค! ๐
์ด ๊ณผ์ ์ ํตํด ๋๋ ํฐํธ๋ฅผ Ubuntu ํ๊ฒฝ์์ Matplotlib์ ์ ์ฉํ ์ ์์ต๋๋ค.
(์ฐธ๊ณ ) Jupyter Snippet
๊ท์ฐฎ์ผ์๋ค๊ณ ์?! ์ ๋ ๊ท์ฐฎ์์ ์ฃผํผํฐ๋ ธํธ๋ถ์์ ํ๋ฒ์ ์คํํ ์ ์๊ฒ ์ฝ๋๋ฅผ ์งฐ์ต๋๋ค ๐
1
2
3
4
5
6
7
8
9
10
11
12
13
# ํ๊ธ ํฐํธ๊ฐ ์์ ๋
! sudo apt-get install fonts-nanum*
import matplotlib
import subprocess
mpl_file = matplotlib.__file__
mpl_file_loc = '/'.join(mpl_file.split('/')[:-1]) + '/mpl-data/fonts/ttf/'
print(matplotlib.__file__)
print(mpl_file_loc)
# ํฐํธ ๋ณต์ฌ
subprocess.run(['cp', '/usr/share/fonts/truetype/nanum/Nanum*', mpl_file_loc], shell=True)
๊ฐ์ํ๊ฒฝ์ ์ฌ์ฉํ์๋ ๋ถ์ ๊ฒฝ์ฐ ๊ฐ์ํ๊ฒฝ๋ง๋ค ์ค์ ์ ํด์ค์ผํ๋ ๋ฒ๊ฑฐ๋ก์์ด ์๊ธดํ์ง๋ง, ํ๊ธ ์ฌ์ฉ์ด ํ์์ ์ด๋ผ๋ฉด ์์ ์๊ฐํด๋๋ฆฐ ๋จ๊ณ๋ฅผ ๋ฐ๋ผ ํฐํธ๋ฅผ ์ค์ ํด๋ณด์ธ์!!
๊ฐ์ฌํฉ๋๋ค ๐