新しい弘大 JupyterHub の matplotlib や sympy-plotting-backends で日本語を表示させる設定

matplotlib でタイトルやラベル・凡例に日本語を表示させる場合には,

import japanize_matplotlib

すればよい,というのが検索すれば出てくる。確かに matplotlib だけならこれでいいが,試してみたところでは新しい弘大 JupyterHub の sympy-plotting-backends では import japanize_matplotlib しても日本語フォント部分がトーフになる。

そんなときは,あらかじめ

fc-list | grep Noto

で Noto フォントがインストールされていることを確認し,~/.config/matplotlib/matplotlibrc に以下のようにフォント設定を書く。

## If you wish to change your default style, copy this file to one of the
## following locations:
## Unix/Linux:
## $HOME/.config/matplotlib/matplotlibrc
## Other platforms:
## $HOME/.matplotlib/matplotlibrc
##
## matplotlib や sympy plotting backends で日本語フォントを使う
##

font.serif: Noto Serif CJK JP, DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif
font.sans-serif: Noto Sans CJK JP, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
font.monospace: Noto Sans Mono CJK JP, DejaVu Sans Mono, Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace

こうすれば,import japanize_matplotlib は不要で日本語がトーフにならずに表示される。

旧 JupyterHub では受講学生の便宜をはかるために管理者権限で system-wide な matplotlibrc を書き換えていたが,新 JupyterHub ではそのような権限もないので,ユーザー毎に設定してもらうほかないが,どうやってこの設定を受講学生にやってもらうかが悩みの種。