LoginSignup
3
3

[Python] matplotlibでグラフ描画時に日本語のラベル名が文字化けする際の対処方法

Posted at

環境

  • MacOS
  • Python 3.10

問題点

  • matplotlibを使ってグラフ化した時に日本語を使うと文字化けする
import matplotlib.pyplot as plt

fruits = {'りんご': 10, 'もも': 20, 'スイカ': 14}
key = list(fruits.keys())
val = list(fruits.values())

plt.bar(key, val)
plt.title('果物の収穫量')

plt.show()

mojibake.png

対処方法

  • matplotlibの日本語パッケージをインストールする
pip install japanize_matplotlib
import matplotlib.pyplot as plt
import japanize_matplotlib # 追加

fruits = {'りんご': 10, 'もも': 20, 'スイカ': 14}
key = list(fruits.keys())
val = list(fruits.values())

plt.bar(key, val)
plt.title('果物の収穫量')

plt.show()

no_mojibake.png

解決!

以上

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