LoginSignup
5
5

More than 3 years have passed since last update.

(19-07-06更新版)Watson Studio Jupyter Notebook上のmatplotlibで日本語ラベルを表示させる

Last updated at Posted at 2017-11-14

(更新履歴)
(2019-07-06) Studio 標準のPythonが3.6に変更になりました。このため、手順前半部分のmatplotlibバージョンアップは不要になりました。逆に後半のところは動かなくなったので、これを機にきれいに書き直しました。
(2018-07-04) 環境設定の手順を追記。手順が間違っていたので修正。
(2018-04-18) OSの構成が変わり、前の手順でうまくいかなくなったことへの対応
(2018-03-27) DSXの名称がWatson Studioに代わり、デフォルトのPython Versionが3.5になったことに対応

デフォルトの、Watson Studio上のJupyter Notebookではmatplotlibで漢字が表示できなくて(いわゆる「豆腐問題」)悲しい思いをします。
この問題に対応する手順を以下に説明します。
テストしているのはPython3.6の環境です。

Notebook作成時のカーネルの選択

最新のStudioでは、Notebook作成時にカーネルのPythonのバージョンを選択できます。ここで下の図のように必ずPython 3.6 XXを選択するようにして下さい。(デフォルトではすでに選択されているはずです。)

スクリーンショット 2019-07-06 10.56.25.png

実行時の手順

下記のコードを、Jupyter Notebookの冒頭セルにコピーした一番最初に起動するようにして下さい。

日本語ラベル表示のためには、上で説明した環境設定だけでなく、以下のコマンドをJupyter Notebook上で追加で行う必要があります。
IBM DSXの時には、一度追加導入したOSイメージはずっと使えたのですが,Watson Studioになって、一度追加導入して修正したOSイメージは数時間すると使えなくなってしまいます。
そのため、以下の手順はWatson Studioを使うたびに行う必要があります。

フォントはライセンスが問題とならないようIPAフォントを利用します。
以下のコマンドでダウンロード、解凍を行い、フォントパスのあるディレクトリにTTFファイルをコピーします。

import os
import shutil
import urllib.request
import zipfile

# IPA フォント関係
ipa_zip_url = 'https://oscdl.ipa.go.jp/IPAexfont/ipaexg00301.zip'
ipa_font_dir = 'ipaexg00301'
ipa_zip_file = 'ipaexg00301.zip'
ipa_font_file = 'ipaexg.ttf'

# mpl ディレクトリ
mpl_dir = '/opt/conda/envs/Python36/lib/python3.6/site-packages/matplotlib/mpl-data'

# フォントキャッシュ
cache_dir = '/home/dsxuser/.cache/matplotlib'

# TTF フォントディレクトリ
ttf_font_dir = os.path.join(mpl_dir, 'fonts', 'ttf')

# ipaフォントパス(zipファイル内)
ipa_font_path = os.path.join(ipa_font_dir, ipa_font_file)

# フォントコピー先パス
dist_path = os.path.join(ttf_font_dir, ipa_font_file)

# matplorlibプロファイルパス
profile_path = os.path.join(mpl_dir, 'matplotlibrc')

# フォントキャッシュクリア
if os.path.exists(cache_dir):
    shutil.rmtree(cache_dir)

# check font path
print('checking %s' %ttf_font_dir)
if os.path.exists(ttf_font_dir):
    print('checking %s' %dist_path)
    if (os.path.exists(dist_path)):
        print('file %s already exist' % dist_path)
    else:
        print('donwload started')
        urllib.request.urlretrieve(ipa_zip_url, ipa_zip_file)
        with zipfile.ZipFile(ipa_zip_file) as f:
            print('unzip started')
            f.extract(ipa_font_path)
        print('copy started')
        shutil.copy(ipa_font_path, dist_path)
        with open(profile_path,'a') as f:
            f.write('font.family : IPAexGothic')
        print( 'IPA font %s copyed! Please restart kernel now.' % ipa_font_file)
else:    
    print('ERROR: font path %s does not exist!' % ttf_font_dir)

カーネル再起動

設定が終わったらカーネルの再起動を行います。

テスト

以下のコードで「豆腐」の文字がグラフ上でも「豆腐」と表示されたら日本語化成功です。
うまくいくと、下の完成図のようになります。

%matplotlib inline
import matplotlib.pyplot as plt
plt.figure()
plt.xlabel('豆腐 - tofu')
plt.title('豆腐 - TOFU')
plt.show()

完成図
スクリーンショット 2017-11-14 16.50.22.png

おまけ
やっとこういうのができるようになりました
スクリーンショット 2017-11-14 17.03.21.png

5
5
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
5
5