LoginSignup
8
6

More than 5 years have passed since last update.

matplotlib, seabornをコマンドのみで最速日本語化(Ubuntu, Google Fonts)

Posted at

目的

文字化け(いわゆる豆腐くん)を
image.png

こうする
image.png

バージョン

# python -V
Python 3.6.5
# pip list | grep -e seaborn -e matplotlib
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
matplotlib (2.2.2)
seaborn (0.8.1)

結論

# 1,Download font
wget https://noto-website-2.storage.googleapis.com/pkgs/NotoSansCJKjp-hinted.zip

# 2,unzip
unzip NotoSansCJKjp-hinted.zip

# 3,Check Directory
python

Python 3.6.5 | packaged by conda-forge | (default, Apr  6 2018, 13:39:56)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.matplotlib_fname()
'/opt/conda/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc'  <- this is config file.

# 4,Copy fontfiles
cp NotoSansCJKjp-*.otf /opt/conda/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/

# 5,Copy config file to home directgory
cp /opt/conda/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc ~/.config/matplotlib/

# 6,Edit font settings(add last row)
vim ~/.config/matplotlib/matplotlibrc
>>> font.serif:Noto Sans CJK JP
>>> font.sans-serif:Noto Sans CJK JP

# 7,seaborn setting
 vim /opt/conda/lib/python3.6/site-packages/seaborn/rcmod.py

>>> # Line80
>>> def set(context="notebook", style="darkgrid", palette="deep",
>>>         font="Noto Sans CJK JP", font_scale=1, color_codes=False, rc=None):

>>> # Line189
>>> "font.family": ["Noto Sans CJK JP"],
>>> "font.sans-serif": ["Noto Sans CJK JP", "Arial", "DejaVu Sans", "Liberation Sans",
                              "Bitstream Vera Sans", "sans-serif"],

# 8,remove font cache
rm -Rf ~/.cache/matplotlib/*

説明

  • matplotlibのフォントは、デフォルトでは日本語対応していない。
  • 今回はGoogleのフォントを利用したが、UbuntuなのでtakaoフォントでもOK。
  • .ttf, .otfファイルなら、その他IPAフォントなどでも可能。ダウンロードと、ファイルの配置の部分だけ読み替えればOK。
  • フォントファイルを置く場所や、matplotlibrc(設定ファイル)の置き場所は環境によって異なる。なのでpythonコマンドで確認している(3,Check Directory)。ちなみに、pythonコマンドを終了するときはCtrl + D。
  • matplotlibはフォントデータをキャッシュする機構があるため、キャッシュを削除しないと反映されない(7,remove font cache)。
  • Jupyter Notebookを起動中の場合は、念のためRestart Kernelすること。
8
6
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
8
6