Google Colabで日本語を使う

develop

1. 何もしないと日本語が文字化けします

 Google ColabでPythonを使い、日本語で説明をつけたグラフを作成したいと思って、やってみましたが、日本語が文字化けしました。日本語を表示するには、日本語フォントをインストールしないといけないようなので、手順を備忘録として残します。

2. システムにあるフォント一覧を取得

import matplotlib.font_manager as fm

# システムにあるフォント一覧を取得
fonts = fm.findSystemFonts()
for font in fonts:
    print(font)

すると以下のようなフォント一覧が出てきます。
ここには、日本語フォントがないようです。

/usr/share/fonts/truetype/liberation/LiberationSerif-Regular.ttf
/usr/share/fonts/truetype/liberation/LiberationMono-Italic.ttf
/usr/share/fonts/truetype/liberation/LiberationMono-BoldItalic.ttf
/usr/share/fonts/truetype/liberation/LiberationSans-Italic.ttf
/usr/share/fonts/truetype/humor-sans/Humor-Sans.ttf
/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf
/usr/share/fonts/truetype/liberation/LiberationSansNarrow-BoldItalic.ttf
/usr/share/fonts/truetype/liberation/LiberationSans-BoldItalic.ttf
/usr/share/fonts/truetype/liberation/LiberationSerif-BoldItalic.ttf
/usr/share/fonts/truetype/liberation/LiberationSerif-Italic.ttf
/usr/share/fonts/truetype/liberation/LiberationSansNarrow-Italic.ttf
/usr/share/fonts/truetype/liberation/LiberationSansNarrow-Regular.ttf
/usr/share/fonts/truetype/liberation/LiberationSansNarrow-Bold.ttf
/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf
/usr/share/fonts/truetype/liberation/LiberationMono-Bold.ttf
/usr/share/fonts/truetype/liberation/LiberationSerif-Bold.ttf
/usr/share/fonts/truetype/liberation/LiberationMono-Regular.ttf

3. 日本語フォントをインストール

以下のコードで日本語フォントをインストールします。

!apt-get update -y
!apt-get install -y fonts-noto-cjk

4. 再度フォント一覧を取得

もう一度フォント一覧を取得してインストールできているか確認します。

import matplotlib.font_manager as fm

# システムにあるフォント一覧を取得
fonts = fm.findSystemFonts()
for font in fonts:
    print(font)

すると以下のように、〜CJK〜と日本語フォントがインストールできていることが確認できます。

/usr/share/fonts/truetype/liberation/LiberationMono-Bold.ttf
/usr/share/fonts/truetype/liberation/LiberationSansNarrow-Bold.ttf
/usr/share/fonts/truetype/liberation/LiberationSerif-Bold.ttf
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc
/usr/share/fonts/truetype/liberation/LiberationMono-BoldItalic.ttf
/usr/share/fonts/truetype/liberation/LiberationSans-Italic.ttf
/usr/share/fonts/truetype/liberation/LiberationSansNarrow-BoldItalic.ttf
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc
/usr/share/fonts/truetype/liberation/LiberationMono-Italic.ttf
/usr/share/fonts/truetype/liberation/LiberationSerif-BoldItalic.ttf
/usr/share/fonts/truetype/liberation/LiberationSansNarrow-Italic.ttf
/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf
/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc
/usr/share/fonts/truetype/liberation/LiberationMono-Regular.ttf
/usr/share/fonts/truetype/liberation/LiberationSerif-Regular.ttf
/usr/share/fonts/truetype/humor-sans/Humor-Sans.ttf
/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf
/usr/share/fonts/truetype/liberation/LiberationSans-BoldItalic.ttf
/usr/share/fonts/truetype/liberation/LiberationSerif-Italic.ttf
/usr/share/fonts/truetype/liberation/LiberationSansNarrow-Regular.ttf

5. 確認する(1回目)

フォントがインストールできたので、日本語が出るか表示してみます。

import matplotlib.pyplot as plt
import matplotlib.font_manager as fm

# フォントのパスを指定
font_path = "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc"

# フォントプロパティを作成
font_prop = fm.FontProperties(fname=font_path)

# matplotlib に適用
plt.rcParams["font.family"] = font_prop.get_name()

# テストプロット
plt.figure(figsize=(5,3))
plt.text(0.5, 0.5, "テスト: 日本語が表示されますか?", fontsize=15, ha='center')
plt.show()

ダメでした。

WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 12486 (\N{KATAKANA LETTER TE}) missing from font(s) DejaVu Sans.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 12473 (\N{KATAKANA LETTER SU}) missing from font(s) DejaVu Sans.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 12488 (\N{KATAKANA LETTER TO}) missing from font(s) DejaVu Sans.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 26085 (\N{CJK UNIFIED IDEOGRAPH-65E5}) missing from font(s) DejaVu Sans.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 26412 (\N{CJK UNIFIED IDEOGRAPH-672C}) missing from font(s) DejaVu Sans.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 35486 (\N{CJK UNIFIED IDEOGRAPH-8A9E}) missing from font(s) DejaVu Sans.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 12364 (\N{HIRAGANA LETTER GA}) missing from font(s) DejaVu Sans.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 34920 (\N{CJK UNIFIED IDEOGRAPH-8868}) missing from font(s) DejaVu Sans.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 31034 (\N{CJK UNIFIED IDEOGRAPH-793A}) missing from font(s) DejaVu Sans.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 12373 (\N{HIRAGANA LETTER SA}) missing from font(s) DejaVu Sans.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 12428 (\N{HIRAGANA LETTER RE}) missing from font(s) DejaVu Sans.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 12414 (\N{HIRAGANA LETTER MA}) missing from font(s) DejaVu Sans.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 12377 (\N{HIRAGANA LETTER SU}) missing from font(s) DejaVu Sans.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 12363 (\N{HIRAGANA LETTER KA}) missing from font(s) DejaVu Sans.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 65311 (\N{FULLWIDTH QUESTION MARK}) missing from font(s) DejaVu Sans.
  fig.canvas.print_figure(bytes_io, **kw)
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.
WARNING:matplotlib.font_manager:findfont: Font family 'Noto Sans CJK JP' not found.

6. フォントキャッシュを削除して再起動する

フォントキャッシュが古いままなので文字化けするようです。そこで、フォントキャッシュを削除して再起動します。

import shutil
import os

# フォントキャッシュを削除
font_cache_path = os.path.expanduser("~/.cache/matplotlib")
if os.path.exists(font_cache_path):
    shutil.rmtree(font_cache_path)

print("フォントキャッシュを削除しました。ランタイムを再起動してください。")

このあと、メニューの「ランタイム」から「セッションを再起動する」を選択してセッションを再起動します。(そのままですね)

7. 確認する(2回目)

再度、トライします。

import matplotlib.pyplot as plt
import matplotlib.font_manager as fm

# フォントのパスを指定
font_path = "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc"

# フォントプロパティを作成
font_prop = fm.FontProperties(fname=font_path)

# matplotlib に適用
plt.rcParams["font.family"] = font_prop.get_name()

# テストプロット
plt.figure(figsize=(5,3))
plt.text(0.5, 0.5, "テスト: 日本語が表示されますか?", fontsize=15, ha='center')
plt.show()

日本語が無事に表示されました。

8. まとめ

簡単にまとめると以下の手順です。

  1. Noto Sans CJK JP フォントをインストール
  2. フォントのパス (/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc) を指定して適用
  3. フォントキャッシュを削除し、ランタイムを再起動

この手順は2025/03/09に確認しました。もっと良い方法があれば、知りたいです。自分でも調べてみます。

9. 追加情報(IPAGothic)

IPAゴシックも確認しました。

!apt-get update -y
!apt-get install -y fonts-ipafont-gothic
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm

# フォントのパスを指定
font_path = '/usr/share/fonts/opentype/ipafont-gothic/ipag.ttf'

# フォントプロパティを作成
font_prop = fm.FontProperties(fname=font_path)
plt.rcParams['font.family'] = font_prop.get_name()

フォントキャッシュを削除し、ランタイムを再起動します。

# テストプロット
plt.figure(figsize=(5, 3))
plt.text(0.5, 0.5, "テスト: 日本語が表示されますか?", fontsize=15, ha='center')
plt.show()

無事に表示されました。

タイトルとURLをコピーしました