LoginSignup
1
0

More than 1 year has passed since last update.

matplotlib と seaborn を日本語化する(Ubuntu)

Last updated at Posted at 2021-10-29

概要

Jupyter で matplotlib や seaborn を使っているとき、ラベル名などに日本語を入力すると、 □ のお豆腐として表示されたりします。

この場合の対処法を記載します。

環境

2021/10/30
matplotlib 3.3.4
seaborn 0.11.1
ubuntu 20.04.3

対処法

日本語フォントをインストール

bash
sudo apt install fonts-ipaexfont

matplotlib の設定ファイルを変更

matplotlib の設定ファイルのパスを表示します。

python
import matplotlib as mpl
mpl.matplotlib_fname()

.../site-packages/matplotlib/mpl-data/matplotlibrcの様なもののはずです。
vi で編集します。

bash
vi .../site-packages/matplotlib/mpl-data/matplotlibrc

vi で開いた状態で :set number をすると行数が表示されます。
251行目の font.family: sans-seriffont.family: IPAexGothic に変更してください。

seaborn の設定ファイルを変更

先ほど表示した matplotlib の設定ファイルのパスの /matplotlib/mpl-data/matplotlibrc の箇所を /seaborn/rcmod.py に変えたものが seaborn の設定ファイルのパスです。

vi で編集します。

bash
vi .../site-packages/seaborn/rcmod.py

vi で開いた状態で :set number をすると行数が表示されます。

87行目の fontIPAexGothic に変えてください。

rcmod.py
def set_theme(context="notebook", style="darkgrid", palette="deep",
              font="IPAexGothic", font_scale=1, color_codes=True, rc=None):

205行目の font.familyIPAexGothic にしてくだい。

rcmod.py
            "font.family": ["IPAexGothic"],

206行目の font.sans-serifIPAexGothic を追加してください。

rcmod.py
            "font.sans-serif": ["Arial", "DejaVu Sans", "Liberation Sans",
                                "Bitstream Vera Sans", "sans-serif", "IPAexGothic"],

matplotlib に関するキャッシュを削除

bash
# キャッシュがある可能性のある場所を全て削除
# 「No such file or directory」とか出たらスキップしてOK
rm -rf ~/.matplotlib/* 
rm -rf ~/.cache/.matplotlib/*
rm -rf ~/.cache/matplotlib/*

確認

jupyter の ipynb でカーネルを再起動すると反映されるはずです。

参考

1
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
1
0