Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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

以上、

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away