0
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

updated at

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

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
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What are the problem?