matplotlibで日本語が文字化けする件
4982 ワード
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()
対応策
↓のフォント設定変数を定義してあげて、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()
以上、
Author And Source
この問題について(matplotlibで日本語が文字化けする件), 我々は、より多くの情報をここで見つけました https://qiita.com/quyasys/items/ca13973be44d9bf3066f著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .