Pythonのmatplotlibの日本語対応で、簡単なモジュールを公開提供されていた方がいた。
方法1
1)日本語フォントのIPAexフォントをダウンロード。
2)ipaexg.ttfというファイルをmatplotlibの設定ファイルが置いてあるフォルダ内
******/lib/python3.7/site-packages/matplotlib/mpl-data/のfonts/ttf/フォルダにコピー
3)matplotlibrcの書き換え
matplotlibrcはmatplotlibデフォルトのグラフのスタイルを指定することができる設定ファイルで、デフォルトはDejaVuSansになっている。
以下のコマンドで読み込まれている、matplotlibrcファイルの場所を調べます。
import matplotlib as mpl
mpl.matplotlib_fname()
>出力結果
/home/****/local/python/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
matplotlibrcを開き、”IPAexGothic”を追加。(コメントアウト解除。)
font.family : IPAexGothic
4)フォントキャッシュの削除
2、方法2
さらに、ネット検索すると、japanize-matplotlibという、matplotlib を日本語表示に対応させるモジュールがGithubに公開されていた。これは簡単!!
<以下その抜粋>
利用方法:
matplotlibをimportした後、japanize_matplotlibをimport
プログラム例:
import matplotlib.pyplot as plt
import japanize_matplotlib
plt.plot([1, 2, 3, 4])
plt.xlabel(‘簡単なグラフ’)
plt.show()
# インストール方法
pip install japanize-matplotlib
利用フォント
IPAフォントのIPAexゴシック(Ver.003.01)を利用しています。 利用にあたってはIPAフォントライセンスv1.0に同意してください。