matplotlib 한글폰트 적용, 마이너스 부호 깨짐 해결하기¶
- ํ๊ธ ํฐํธ ์ ์ฉํ๋ ๋ฐฉ๋ฒ์ 3๊ฐ์ง ์ ๋๊ฐ ์๋ค.
- ์๋ 3๊ฐ์ง ๋ฐฉ๋ฒ ์ค ๊ธฐํธ์ ๋ง๊ฒ ์ฌ์ฉํ๋ฉด ๋๋ค.
In [1]:
import seaborn as sns
import matplotlib as mpl
from matplotlib import font_manager as fm
from matplotlib import pyplot as plt
from matplotlib import rc
1. FontProperties¶
- matplotlib์ font_manager ๊ฐ์ฒด๋ฅผ ์ด์ฉํด์ ํฐํธ ๊ฐ์ฒด๋ฅผ ๋ง๋ ํ plot์์ ํ์ํ ๋ถ๋ถ์ ์ ์ฉํ๋ค.
- ๊ทธ๋ํ์์ ๋ถ๋ถ๋ณ๋ก ํฐํธ๋ฅผ ์ ์ฉํ ์ ์๋ค.
- ๋งค๋ฒ ์ ์ฉํด์ค์ผํ๋ ๋ถํธํจ์ด ์๋ค.
In [40]:
# ํฐํธ ํน์ฑ setup
font_path_bitro = '/Users/simon/Library/Fonts/แแ
ตแแ
ณแ
แ
ฉ แแ
ฉแแ
ฅ OTF.otf'
font_path_sc2 = '/Users/simon/Library/Fonts/SCDream2.otf'
fp_tick_label = fm.FontProperties(fname=font_path_sc2, size=10)
fp_label = fm.FontProperties(fname=font_path_bitro, size=14)
fp_title = fm.FontProperties(fname=font_path_bitro, size=18)
# ๊ทธ๋ํ ๊ทธ๋ฆฌ๊ธฐ
plt.figure(figsize=(8, 6)) # ๊ฐ๋ก, ์ธ๋ก, ๋จ์: inch
sns.barplot(data=jeju, x='TotalSpent', y='Category')
# data๋ ๊ฐ์์ ๋ฐ์ดํฐ๋ฅผ ์ฐ๋ฉด ๋ฉ๋๋ค.
# ์ ๋ ๊ณต๋ถํ๊ณ ์๋ ์ ์ฃผ๋ ์์๊ณต์ธ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํด์ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ ธ์ต๋๋ค.
plt.title('์นดํ
๊ณ ๋ฆฌ ๋ณ ์ด ์๋น ํฉ', fontproperties=fp_title)
plt.xlabel('์ด ์๋น', fontproperties=fp_label)
plt.ylabel('์นดํ
๊ณ ๋ฆฌ', fontproperties=fp_label)
ax = plt.subplot()
for label in ax.get_xticklabels():
label.set_fontproperties(fp_tick_label)
for label in ax.get_yticklabels():
label.set_fontproperties(fp_tick_label)
<ipython-input-40-55ab080e6fcf>:19: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance. ax = plt.subplot()
2. rcParams 를 이용해서 전역 범위 설정¶
- ์ด๋ ๊ฒ ํ๋ฉด ์ฃผํผํฐ ๋ ธํธ๋ถ์ ์ ์ญ ๋ฒ์์ ์ ์ฉ๋จ.
- ๋ํดํธ ๊ฐ์ผ๋ก ์ค์ ํด๋๊ณ ์ฐ๋ฉด ์ข์ ๋ฏ
In [10]:
import seaborn as sns
import matplotlib as mpl
from matplotlib import font_manager as fm
from matplotlib import pyplot as plt
from matplotlib import rc
In [11]:
plt.rcParams["font.family"] = "esamanru OTF" # ํฐํธ ์ด๋ฆ ์ค์
plt.rcParams['font.size'] = 12
plt.rcParams['axes.unicode_minus'] = False # ๋ง์ด๋์ค ๋ถํธ ๊นจ์ง ํด๊ฒฐ
# ํฐํธ ์ธ์ rcParams ๊ฐ์ฒด๋ฅผ ๋ณด๋ฉด ๋ฐ๊ฟ ์ ์๋ ๊ฐ๋ค์ด ๋ง๋ค.
# plt.rcParams.keys() ๋ก ๋ชจ๋ key ๋ชฉ๋ก์ ๋ณผ ์ ์์.
# plot์ ํฌ๊ธฐ๋ ์ค์ ๊ฐ๋ฅ
plt.rcParams['figure.figsize'] = (8,6)
- ์ ๋ฐฉ๋ฒ๋ ๋๊ณ rc๋ฅผ ์ด์ฉํ ์๋ ์๋ค.
In [5]:
font_list = fm.findSystemFonts() # ์์คํ
์ ์ค์น๋ ํฐํธ๋ค์ ๊ฒฝ๋ก๋ฅผ ๋ถ๋ฌ์จ๋ค.
for fp in font_list:
if 'Nanum' in fp:
font_path = fp
print(font_path)
font = fm.FontProperties(fname=font_path).get_name()# ํฐํธ์ ๊ฒฝ๋ก ์ง์
rc('font', family=font)
/Users/jeongjaeyong/Library/Fonts/NanumGothic.ttf
3. 노트북 자체 설정에 적용하기¶
- ๋ ธํธ๋ถ์ ๊ป๋ค๊ฐ ์คํํ ๋ ์๋์ผ๋ก ์ ์ฉ
- ๋ชจ๋ ๋ ธํธ๋ถ์ ๊ณตํต์ผ๋ก ์ ์ฉ๋จ
In [9]:
# matplotlib ์ค์ ํ์ผ์ ๊ฒฝ๋ก
mpl.matplotlib_fname()
Out[9]:
'/Users/jeongjaeyong/anaconda3/lib/python3.8/site-packages/matplotlib/mpl-data/matplotlibrc'
- ์ ์ค์ ํ์ผ ๊ฒฝ๋ก๋ก ์ด๋ํด์ ctrl-F / cmd-F ๋ก font.family๋ฅผ ์ฐพ๋๋ค.
- ์ค์ ํ๊ณ ์ถ์ ํฐํธ ์ด๋ฆ์ผ๋ก ๋์ฒดํ๋ค.
์ด๋ฆ ๋ค์ ttf๋ otf๋ ๋นผ๊ณ ์ธ ๊ฒ
๋ง์ด๋์ค ๋ถํธ ๊นจ์ง์ axes.unicode_minus = False๋ก ๋ฐ๊พธ๋ฉด ํด๊ฒฐ๋๋ค.
잘 안되는 경우¶
- ํฐํธ ๋งค๋์ ๊ฐ ํฐํธ๋ฅผ ๋ถ๋ฌ์ค์ง ๋ชปํ์ ๊ฐ๋ฅ์ฑ
In [67]:
for f in fm.fontManager.ttflist:
if 'esamanru OTF' in f.fname:
print(f)
<Font 'esamanru OTF' (esamanru OTF Light.otf) normal normal 300 normal> <Font 'esamanru OTF' (esamanru OTF Bold.otf) normal normal 700 normal> <Font 'esamanru OTF' (esamanru OTF Medium.otf) normal normal 500 normal>
- ์์์ ํฐํธ ๋งค๋์ ์ ์ํ๋ ํฐํธ๊ฐ ์์ผ๋ฉด ํฐํธ ๋งค๋์ ์๋ก๊ณ ์นจ ํด๋ณด๊ธฐ
In [13]:
fm._rebuild()
- matplotlib ์บ์ ์ญ์ ํด๋ณด๊ธฐ
In [60]:
# matplotlib ์บ์ ๊ฒฝ๋ก
matplotlib.get_cachedir()
Out[60]:
'/Users/simon/.matplotlib'
- ์ ๊ฒฝ๋ก๋ก ๊ฐ์ ์บ์ ํ์ผ ์ญ์ , json ํ์ผ๋ ์ญ์
- ๋ ธํธ๋ถ ์ฌ์๋
- ๊ทธ๋๋ ์๋๋ฉด ํฐํธ ์ด๋ฆ์ด ์ ๋๋ก ๋๋์ง ํ์ธํ๊ธฐ.
'๐ ๋ฐ์ดํฐ๋ถ์' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[python] ํ์ด์ฌ ๊ฐ์ฒด ์ง๋ ฌํ (Serialization) (0) | 2021.10.31 |
---|