5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

matplotlibで日本語が文字化けする件

Last updated at Posted at 2018-11-29

matplotlibで凡例やタイトルに日本語を表示させる場合文字化けします。
ぐぐると対策としてmatplotlibrcという設定ファイルをいじる方法が紹介されていますが、、
それでも文字化けがなおらない人向けの 苦肉の策 を紹介します。

普通にグラフ描画→文字化けパターン

import numpy as np
import matplotlib.pyplot as plt
 
left = np.array([1, 2, 3, 4, 5])
height = np.array([100, 200, 300, 400, 500])
plt.bar(left, height)
plt.show()

↓見事にタイトル文字化け
Figure_1.png

対応策

↓のフォント設定変数を定義してあげて、plt.titleに渡してあげると文字化けが解消します。
Windows標準だと↓のパスがそのまま使えると思いますが、使えない人は正しくフォント設定ファイルのパスを指定してください。

from matplotlib.font_manager import FontProperties
fp = FontProperties(fname=r'c:\Windows\Fonts\meiryo.ttc')

修正版グラフ描画→文字化け解消パターン

import numpy as np
import matplotlib.pyplot as plt  
'from matplotlib.font_manager import FontProperties'

fp = FontProperties(fname=r'c:\Windows\Fonts\meiryo.ttc')

left = np.array([1, 2, 3, 4, 5])
height = np.array([100, 200, 300, 400, 500])
plt.bar(left, height)
plt.title('日本語タイトル',fontproperties=fp)
plt.show()

Figure_2.png

以上、

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?