LoginSignup
35
27

More than 5 years have passed since last update.

Colaboratoryでmatplotlibの日本語表示

Last updated at Posted at 2018-09-14

はじめに

Googleが機械学習の教育や研究用に提供しているColaboratoryを試してみた。
即座にjupyter環境が使えていい感じだったが、matplotlibで日本語を使うのに少し詰まったのでメモ。

Colaboratory - よくある質問

日本語フォントをインストール

Colaboratoryはapt-getが使えるのでセルに

!apt-get -y install fonts-ipafont-gothic

などと打ち込み実行。

キャッシュを消す

フォントキャッシュの場所を確認

import matplotlib
matplotlib.get_cachedir()

を実行すると

'/root/.cache/matplotlib'

などとかえってくる。
ここにあるfontList.jsonというのがフォントのキャッシュなので

rm /root/.cache/matplotlib/fontList.json

などとして一旦消す。

ランタイムをリスタート

一度ランタイムをリスタートする。

再度描画

import matplotlib.pyplot as plt

jp_font = {'fontname':'IPAGothic'}

plt.plot([0, 1], [0, 1])
plt.xlabel('横軸', **jp_font)
plt.ylabel('縦軸', **jp_font)
plt.title('タイトル', **jp_font)
plt.show()

や、seabornを使って

import matplotlib.pyplot as plt
import seaborn as sns
sns.set(font='IPAGothic')

plt.plot([0, 1], [0, 1])
plt.xlabel('横軸')
plt.ylabel('縦軸')
plt.title('タイトル')
plt.show()

といった感じで試しに描画してみると

image.png

日本語が使える。

参考

35
27
1

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
35
27