• home
  • about
  • 全ての投稿
  • ソフトウェア・ハードウェアの設定のまとめ
  • 分析関連のまとめ
  • ヘルスケア関連のまとめ
  • 生涯学習関連のまとめ

matplotlib/seaborn font

date: 2024-05-31 excerpt: matplotlib/seaborn のフォント設定メモ

tag: seabornpythonmatplotlib可視化


matplotlib/seaborn font

seaborn特有の問題点

  • フォントを設定したあとも
    • sns.set(), sns.set_theme() でフォントが上書きされることがある
  • 回避策
    • sns.set() / sns.set_theme() の呼び出し後に plt.rcParams['font.family'] を再設定する
    • または sns.set_theme(font='Noto Sans CJK JP') のようにフォントを明示する

Ubuntuで日本語フォントを使う

フォントのインストール & キャッシュクリア

$ sudo apt update && sudo apt install -y fonts-noto-cjk
$ rm -rf ~/.cache/matplotlib 
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
print(f"rcParams = {matplotlib.rcParams['font.family']}")

# 日本語フォントを直接指定
plt.rcParams['font.family'] = 'Noto Sans CJK JP'
# マイナス記号の文字化け対策
plt.rcParams['axes.unicode_minus'] = False

sns.barplot(x=["日本語"], y=[1])
plt.title("日本語タイトル")
plt.show()

Windowsで日本語フォントを使う

import matplotlib.pyplot as plt
import seaborn as sns
import os

if os.name == 'nt':
    sns.set(font='Yu Gothic')


seabornpythonmatplotlib可視化 Share Tweet