LoginSignup
2
0

More than 3 years have passed since last update.

Jupyterのmatplotlibを使用する際の日本語豆腐を解決

Last updated at Posted at 2019-05-30

TL;NR

dockerでmatplotlibの文字化けと戦う

環境:

docker on ubuntu18.04
python3

課題:

image.png

アクション:

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
image.png

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

image.png

恒久対応

matplotlibrc設定ファイルを修正

cp /usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/matplotlibrc ~/.config/matplotlib/

ファイル内のFontセッションを修正

font.family         : IPAGothic
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0