🪺

PyPlot.jl で日本語フォントを使う方法

に公開

はじめに

Julia で図を作成するとき, Plots.jl がよく使われます.
しかし, Plots.jl だと日本語フォントがうまく扱えません. (少なくとも私は方法を見つけられていません...)

PyPlot.jl だと設定すれば日本語フォントが使えます.
その設定をここに書いておきます.

PyPlot.jl の設定

処理の最初で以下を実行します.
IPAGothic を使う場合は次のようにします.
また, などの文字が IPAGothic には入っていないので DejaVu Sans を二番目に指定しています.

using PyCall

font_manager = pyimport("matplotlib.font_manager")
font_manager.fontManager.addfont("/usr/share/fonts/truetype/fonts-japanese-gothic.ttf")
PyCall.PyDict(matplotlib["rcParams"])["font.family"] = ["IPAGothic", "DejaVu Sans"]

これで using PyPlot して set_title などに日本語を含む文字列を入れても正常に表示されます.

実行環境

$ julia --version
julia version 1.11.2
$ julia -e 'using Pkg; Pkg.status()'
Status `~/.julia/environments/v1.11/Project.toml`
  [8f4d0f93] Conda v1.10.2
  [d330b81b] PyPlot v2.11.5
$ fc-list
/usr/share/fonts/truetype/dejavu/DejaVuSerif-Bold.ttf: DejaVu Serif:style=Bold
/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf: DejaVu Sans Mono:style=Book
/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf: DejaVu Sans:style=Book
/usr/share/fonts/opentype/ipafont-mincho/ipam.ttf: IPAMincho,IPA明朝:style=Regular
/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Oblique.ttf: DejaVu Sans Mono:style=Oblique
/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf: DejaVu Sans:style=Bold
/usr/share/fonts/truetype/dejavu/DejaVuSansMono-BoldOblique.ttf: DejaVu Sans Mono:style=Bold Oblique
/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf: DejaVu Sans Mono:style=Bold
/usr/share/fonts/opentype/ipafont-gothic/ipag.ttf: IPAGothic,IPAゴシック:style=Regular
/usr/share/fonts/truetype/fonts-japanese-mincho.ttf: IPAMincho,IPA明朝:style=Regular
/usr/share/fonts/truetype/fonts-japanese-gothic.ttf: IPAGothic,IPAゴシック:style=Regular
/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf: DejaVu Serif:style=Book
/usr/share/fonts/opentype/ipafont-mincho/ipamp.ttf: IPAPMincho,IPA P明朝,䥐䅐䵩湣桯:style=Regular
/usr/share/fonts/opentype/ipafont-gothic/ipagp.ttf: IPAPGothic,IPA Pゴシック,䥐䅐䝯瑨楣:style=Regular
$ screenfetch
                          ./+o+-       root@93ee09ab3060
                  yyyyy- -yyyyyy+      OS: Ubuntu 
               ://+//////-yyyyyyo      Kernel: aarch64 Linux 6.10.14-linuxkit
           .++ .:/++++++/-.+sss/`      Uptime: 19h 18m
         .:++o:  /++++++++/:--:/-      Packages: 260
        o:+o+:++.`..```.-/oo+++++/     Shell: zsh 5.9
       .:+o:+o/.          `+sssoo+/    Disk: 538G / 578G (94%)
  .++/+:+oo+o:`             /sssooo.   CPU: 8x Apple -
 /+++//+:`oo+o               /::--:.   RAM: 2678MiB / 13709MiB
 \+/+o+++`o++o               ++////.  
  .++.o+++oo+:`             /dddhhh.  
       .+.o+oo:.          `oddhhhh+   
        \+.++o+o``-````.:ohdhhhhh+    
         `:o+++ `ohhhhhhhhyo++os:     
           .o:`.syhhhhhhh/.oo++o`     
               /osyyyyyyo++ooo+++/    
                   ````` +oo+++o\:    
                          `oo++.

Discussion