2019年5月28日 星期二

install chinese and japanese fonts for matplotlib and seaborn plots

import matplotlib
from matplotlib.font_manager import FontProperties

### 下載日中字型檔
### install japanese font
!apt-get -y install fonts-ipafont-gothic
font_jp = FontProperties(fname=r'/usr/share/fonts/opentype/ipafont-gothic/ipagp.ttf',size=20)
print(font_jp.get_family())
print(font_jp.get_name())

### install chinese font
!apt-get -y install fonts-moe-standard-kai 
font_tw = FontProperties(fname=r'/usr/share/fonts/truetype/moe/MoeStandardKai.ttf',size=20)
print(font_tw.get_family())
print(font_tw.get_name())

### install chinese,japanese,korean font
#!apt-get install fonts-noto-cjk  ## .ttc
#font_cjk = FontProperties(fname=r'/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc',size=20)
#print(font_cjk.get_family())
#print(font_cjk.get_name())

!apt-get install ttf-unifont
font_uni = FontProperties(fname=r'/usr/share/fonts/truetype/unifont/unifont.ttf',size=20)
print(font_uni.get_family())
print(font_uni.get_name())

### 設定畫圖啟用 sans-serif 系列字型
!grep font.family /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/matplotlibrc
!sed -i "s/#font.family/font.family/" /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/matplotlibrc
!grep font.family /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/matplotlibrc

### 設定屬於 sans-serif 系列字型包含日中字型
!grep font.sans-serif /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/matplotlibrc
!sed -i "s/#font.sans-serif.*DejaVu Sans/font.sans-serif     : IPAPGothic, TW-MOE-Std-Kai, Unifont, DejaVu Sans/" /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/matplotlibrc
#!sed -i "s/font.sans-serif.*DejaVu Sans/font.sans-serif     : IPAPGothic, TW-MOE-Std-Kai, Unifont, DejaVu Sans/" /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/matplotlibrc
!grep font.sans-serif /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/matplotlibrc

### 連結日中字型檔到畫圖字型目錄
!ln -s /usr/share/fonts/opentype/ipafont-gothic/ipagp.ttf /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf/
!ln -s /usr/share/fonts/truetype/moe/MoeStandardKai.ttf /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf/
#!ln -s /usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf/
!ln -s /usr/share/fonts/truetype/unifont/unifont.ttf /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf/
!ls /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf/[Miu]*

### 重建畫圖字型快取,納入日中字型檔
matplotlib.font_manager._rebuild()
flist = matplotlib.font_manager.get_fontconfig_fonts()
names = [matplotlib.font_manager.FontProperties(fname=fname).get_name() for fname in flist]
print(names)

### 確認sans-serif系列清單有日中字型
print(matplotlib.rcParams['font.sans-serif'])  ## 確認sans-serif系列清單是否有日中字型
if 'IPAPGothic' not in matplotlib.rcParams['font.sans-serif']:
  matplotlib.rcParams['font.sans-serif'] = ['IPAPGothic', 'TW-MOE-Std-Kai', 'Unifont'] + matplotlib.rcParams['font.sans-serif']
print(matplotlib.rcParams['font.sans-serif'])

### 測試畫圖字型顯示
#!!!!! 若X軸標示字型有誤,表示引擎快取仍未更新,請【重新啟動並運行所有單元格】
import matplotlib.pyplot as plt

testString = u"喜欢 海灘 散步 걷기 好き"   ## 簡中,繁中,日文,韓文,日文
plt.title(testString, fontproperties=font_uni)
plt.xlabel(testString)   # 利用sans-serif第一個字型顯示
plt.ylabel(testString, fontproperties=font_tw)
plt.show()
import seaborn as sns
emotion_counter = [('愉快', 200), ('高興', 180), ('開心', 160), ('歡喜', 140), ('生氣', 130), ('憤怒', 120), ('悲傷', 110), ('難過', 100), ('哀愁', 90), ('傷感', 80)]
sns.set_color_codes("pastel")
sns.barplot(x=[k for k, _ in emotion_counter], y=[v for _, v in emotion_counter])
參考: 解決Python 3 Matplotlib與Seaborn視覺化套件中文顯示問題 link

沒有留言: