サブロウ丸

Sabrou-mal サブロウ丸

主にプログラミングと数学

Matplotlib 日本語の使用

Pythonの代表的なグラフ描写モジュールmatplotlibでグラフタイトルや凡例に日本語を使う方法をメモります。

環境:
OS: macOS High Sierra 10.13.3
Python: Python3.6

基本的には, 以下のサイトを参考にすれば上手くいくと思いますが

/usr/local/lib/python3.6/site-packages/matplotlib/font_manager.py:1320: UserWarning: findfont: Font family ['IPAexGothic'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

というエラーが起きてしまいました。
上記のサイトでも言われている通り,~/.matplotlib内の casheファイルfontList.py3k.cacheを削除しても同様のエラーは消えず。。。

結論としては, 同ディレクトリ内のfont-listファイルfontList.jsonも削除する必要がありました。
削除した後で, matplotlibで描写をさせると

/usr/local/lib/python3.6/site-packages/matplotlib/font_manager.py:281: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  'Matplotlib is building the font cache using fc-list. '

という表示が。
しばらく待つと新しいfont-listファイルが作成されて, 無事に日本語が表示されました。
f:id:inarizuuuushi:20180126143439p:plain

めでたし。