LoginSignup
12
11

More than 3 years have passed since last update.

[Python] Ubuntuでmatplotlibの日本語化

Posted at

はじめに

Pythonでクローリングとスクレイピングを勉強しているのだが、本の途中で出てきたmatplotlibの日本語化がスムーズにできなかったのでまとめておきます

環境

Windows10上のvirtualboxでUbuntuの仮想環境
Ubuntu 18.04.4 LTS

経緯

  • matplotlibのインストール

環境によってことなるので注意してください
コマンドで以下を実行

pip install matplotlib
  • 日本語フォントをUbuntuにインストール
sudo apt install -y fonts-migmix #Ubuntuの場合
  • 日本語をラベル名にして図を描画
japanese_label.py
import matplotlib
matplotlib.use("Agg")

import matplotlib.pyplot as plt

plt.plot([1,2,3,4,5],[1,2,3,4,5], "bx-", label="1次関数")
plt.plot([1,2,3,4,5],[1,4,9,16,25],"ro--", label="2次関数")

plt.xlabel("xの値")
plt.ylabel("yの値")

plt.legend(loc="best")
plt.xlim(0, 6)
plt.savefig("japanese_label.png", dpi=300)

(参考文献)Pythonクローリング&スクレイピング ―データ収集・解析のための実践開発ガイド―

このようにすると図がカレントディレクトリに保存されますが以下のように日本語部分が□で表示されてしまいます
japanese_label.png

これを解消したい

解決方法

こちらの記事を参考にしました
matplotlibで日本語を描画 on Ubuntu

  • インストールした日本語フォントの在処を確認
fc-list | grep migmix

/usr/share/fonts/truetype/migmix/migmix-2m-bold.ttf: MigMix 2M:style=Bold
/usr/share/fonts/truetype/migmix/migmix-2p-bold.ttf: MigMix 2P:style=Bold
/usr/share/fonts/truetype/migmix/migu-1m-regular.ttf: Migu 1M:style=Regular
/usr/share/fonts/truetype/migmix/migu-2m-regular.ttf: Migu 2M:style=Regular
/usr/share/fonts/truetype/migmix/migu-1c-regular.ttf: Migu 1C:style=Regular
/usr/share/fonts/truetype/migmix/migu-1p-regular.ttf: Migu 1P:style=Regular
/usr/share/fonts/truetype/migmix/migmix-1p-bold.ttf: MigMix 1P:style=Bold
/usr/share/fonts/truetype/migmix/migmix-2m-regular.ttf: MigMix 2M:style=Regular
/usr/share/fonts/truetype/migmix/migmix-1m-regular.ttf: MigMix 1M:style=Regular
/usr/share/fonts/truetype/migmix/migmix-1p-regular.ttf: MigMix 1P:style=Regular
/usr/share/fonts/truetype/migmix/migmix-2p-regular.ttf: MigMix 2P:style=Regular
/usr/share/fonts/truetype/migmix/migu-2m-bold.ttf: Migu 2M:style=Bold
/usr/share/fonts/truetype/migmix/migu-1p-bold.ttf: Migu 1P:style=Bold
/usr/share/fonts/truetype/migmix/migu-1c-bold.ttf: Migu 1C:style=Bold
/usr/share/fonts/truetype/migmix/migu-1m-bold.ttf: Migu 1M:style=Bold
/usr/share/fonts/truetype/migmix/migmix-1m-bold.ttf: MigMix 1M:style=Bold

フォントはインストールされているようですね。ではこれらのうち使いたいフォントのパスを確認してください。

  • matplotlibでフォントを使えるようにする
japanese_label.py
import matplotlib
matplotlib.use("Agg")

# 先ほどのコードに以下4行を追加
from matplotlib.font_manager import FontProperties
font_path = "/usr/share/fonts/truetype/migmix/migmix-1p-regular.ttf"
font_prop = FontProperties(fname=font_path)
matplotlib.rcParams["font.family"] = font_prop.get_name()

import matplotlib.pyplot as plt

plt.plot([1,2,3,4,5],[1,2,3,4,5], "bx-", label="1次関数")
plt.plot([1,2,3,4,5],[1,4,9,16,25],"ro--", label="2次関数")

plt.xlabel("xの値")
plt.ylabel("yの値")

plt.legend(loc="best")
plt.xlim(0, 6)
plt.savefig("japanese_label.png", dpi=300)

保存した図
japanese_label.png

日本語が表示されていますね

これでもできないとき

matplotlibが自動で作る(?)キャッシュファイルが原因の場合があります。私もここでつまずきました。matplotlibのキャッシュファイルは~/.cache/matplotlibにありました。

以下のコマンドを実行して中身を確認してキャッシュファイルと思われるものを削除してください
terminal
ls ~/.cache/matplotlib/

私の場合はrm ~/.cache/matplotlib/tex.cacheでキャッシュファイルを削除できました。

最後に

キャッシュファイルのせいで思ったように動かないのはあるあるらしいですね。覚えておきます。
ちなみに初投稿でした。読んでいただきありがとうございます。

12
11
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
12
11