TL;NR
dockerでmatplotlibの文字化けと戦う
環境:
docker on ubuntu18.04
python3
課題:
アクション:
matplotlibのfont.familyのフォントを指定
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'IPAGothic'
plot somthing
UserWarning: findfont: Font family ['IPAGothic'] not found. Falling back to DejaVu Sans(prop.get_family(), self.defaultFamily[fontext]))
fontキャッシュ削除
rm ~/.cache/matplotlib/fontList.*
fontダウンロード(今回はIPAGothicを使用)
https://ipafont.ipa.go.jp/old/ipafont/download.html
matplotlibのfontPathを特定
print(matplotlib.matplotlib_fname())
/usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/matplotlibrc
ファイルを展開し.ttfファイルをdockerのfontPathにcopy
docker cp ipag.ttf コンテナNAME:/usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/fonts/ttf/
Done
恒久対応
matplotlibrc設定ファイルを修正
cp /usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/matplotlibrc ~/.config/matplotlib/
ファイル内のFontセッションを修正
font.family : IPAGothic