プログラミングとかLinuxとかの備忘録

プログラミング、Linuxでハマった箇所や環境構築のメモ

matplotlibでNimbus Roman No9 Lを使う

スポンサーリンク

でグラフの文字をNimbus Roman No9 Lにしたい

フォントのインストール

$ sudo apt -y install fonts-texgyre

使用するコード

test.mplstyleは空のテキスト

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt

with plt.style.context('test.mplstyle'):
    x = np.linspace(0,3,20)
    y = x**2 + 1

    plt.plot(x, y, "r-")
    plt.xlabel("x")
    plt.ylabel("y")

    plt.savefig("test.pdf")

プロットしてみる

この時点ではフォント設定を変更していないのでデフォルトのフォントが使用される

$ python3 plot.py
$ pdffonts test.pdf
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
DejaVuSans                           Type 3            Custom           yes no  no      14  0

フォントを変える

font.serifにNimbus Roman No9 Lを指定

$ vim test.mplstyle
font.serif  : Nimbus Roman No9 L
font.family : serif

して実行

$ python3 plot.py
...
UserWarning: findfont: Font family ['serif'] not found. Falling back to DejaVu Sans
...

$ pdffonts test.pdf
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
DejaVuSans                           Type 3            Custom           yes no  no      14  0

serifが見つからないらしい

font.familyに直接フォントを指定

$ vim test.mplstyle
font.family  : Nimbus Roman No9 L

して実行

$ python3 plot.py
...
UserWarning: findfont: Font family ['Nimbus Roman No9 L'] not found. Falling back to DejaVu Sans
...

$ pdffonts test.pdf
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
DejaVuSans                           Type 3            Custom           yes no  no      14  0

Nimbus Roman No9 Lが見つからない

Nimbus Roman No9 Lはインストールされているのか確認

インストールはされている

$ fc-list | grep Nimbus
/usr/share/fonts/type1/gsfonts/n021024l.pfb: Nimbus Roman No9 L:style=Medium Italic
/usr/share/fonts/type1/gsfonts/n021004l.pfb: Nimbus Roman No9 L:style=Medium
/usr/share/fonts/type1/gsfonts/n022003l.pfb: Nimbus Mono L:style=Regular
/usr/share/fonts/type1/gsfonts/n021023l.pfb: Nimbus Roman No9 L:style=Regular Italic
/usr/share/fonts/type1/gsfonts/n019063l.pfb: Nimbus Sans L:style=Regular Condensed Italic
/usr/share/fonts/type1/gsfonts/n019064l.pfb: Nimbus Sans L:style=Bold Condensed Italic
/usr/share/fonts/type1/gsfonts/n022023l.pfb: Nimbus Mono L:style=Regular Oblique
/usr/share/fonts/type1/gsfonts/n019043l.pfb: Nimbus Sans L:style=Regular Condensed
/usr/share/fonts/type1/gsfonts/n019044l.pfb: Nimbus Sans L:style=Bold Condensed
/usr/share/fonts/type1/gsfonts/n021003l.pfb: Nimbus Roman No9 L:style=Regular
/usr/share/fonts/type1/gsfonts/n019023l.pfb: Nimbus Sans L:style=Regular Italic
/usr/share/fonts/type1/gsfonts/n022004l.pfb: Nimbus Mono L:style=Bold
/usr/share/fonts/type1/gsfonts/n019024l.pfb: Nimbus Sans L:style=Bold Italic
/usr/share/fonts/type1/gsfonts/n019004l.pfb: Nimbus Sans L:style=Bold
/usr/share/fonts/type1/gsfonts/n022024l.pfb: Nimbus Mono L:style=Bold Oblique
/usr/share/fonts/type1/gsfonts/n019003l.pfb: Nimbus Sans L:style=Regular

matplotlibのキャッシュを削除

$ rm -rf ~/.cache/matplotlib

ここでもう一度プロットするもNimbus Roman No9 Lが見つからない

$ python3 plot.py
...
UserWarning: findfont: Font family ['Nimbus Roman No9 L'] not found. Falling back to DejaVu Sans
...

$ pdffonts test.pdf
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
DejaVuSans                           Type 3            Custom           yes no  no      14  0

texモードをONにする

$ vim test.mplstyle
text.usetex : true
font.serif  : Nimbus Roman No9 L
font.family : serif

DejaVuではなくなったが、Nimbus Romanでもない

$ python3 plot.py
...
UserWarning: findfont: Font family ['Nimbus Roman No9 L'] not found. Falling back to DejaVu Sans
...

$ pdffonts test.pdf
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
CMMI10                               Type 1            Builtin          yes no  no      17  0
CMR10                                Type 1            Builtin          yes no  no      13  0

なんとなくTimesにしてみたら

$ vim test.mplstyle
text.usetex : true
font.serif  : Times
font.family : serif

縦軸、横軸の文字(xy)がNimbus Romanになった

$ python3 plot.py
$ pdffonts test.pdf
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
CMMI10                               Type 1            Builtin          yes no  no      17  0
NimbusRomNo9L-Regu                   Type 1            Custom           yes no  no      13  0

texモードをOFFにする

$ vim test.mplstyle
text.usetex : false
font.family : Times

DejaVu

$ python3 plot.py
...
UserWarning: findfont: Font family ['Nimbus Roman No9 L'] not found. Falling back to DejaVu Sans
...

name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
DejaVuSans                           Type 3            Custom           yes no  no      14  0

まとめ

matplotlibでNimbus Roman No9 Lを使用するときは

fonts-texgyreをインストールして、matplotlibのフォント設定を

text.usetex : true
font.serif  : Times
font.family : serif

にする