hack のためのネタ帳, etc,,,

状況

local の HTML で PyScript を使って matplotlib で日本語を使おうとしたのだが、 の方法が通用しなかった。

まず、
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<py-config>
packages = ["matplotlib"]
</py-config>
<py-script>
import matplotlib.pyplot as plt

plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = [
    'Hiragino Maru Gothic Pro',
    'Yu Gothic',
    'Meirio',
    'Takao',
    'IPAexGothic',
    'IPAPGothic',
    'VL PGothic',
    'Noto Sans CJK JP',
]

fig, ax = plt.subplots()
ax.plot(range(10))
ax.set_title("matplotlib で日本語")
display(fig)
</py-script>
</body>
</html>
のように、rcParams で font.famyli 設定する方法だと一応動きはするんだけど、以下のようなエラーで指定したフォントが見つからないって言われて日本語が豆腐(□)になる。
findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans.
findfont: Generic family 'sans-serif' not found because none of the following families were found: Hiragino Maru Gothic Pro, Yu Gothic, Meirio, Takao, IPAexGothic, IPAPGothic, VL PGothic, Noto Sans CJK JP
/home/pyodide/pyscript/_mime.py:67: UserWarning: Glyph 12391 (\N{HIRAGANA LETTER DE}) missing from current font.
  obj.savefig(buf, format="png")
/home/pyodide/pyscript/_mime.py:67: UserWarning: Glyph 26085 (\N{CJK UNIFIED IDEOGRAPH-65E5}) missing from current font.
  obj.savefig(buf, format="png")
/home/pyodide/pyscript/_mime.py:67: UserWarning: Glyph 26412 (\N{CJK UNIFIED IDEOGRAPH-672C}) missing from current font.
  obj.savefig(buf, format="png")
/home/pyodide/pyscript/_mime.py:67: UserWarning: Glyph 35486 (\N{CJK UNIFIED IDEOGRAPH-8A9E}) missing from current font.
  obj.savefig(buf, format="png")
findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans.
findfont: Generic family 'sans-serif' not found because none of the following families were found: Hiragino Maru Gothic Pro, Yu Gothic, Meirio, Takao, IPAexGothic, IPAPGothic, VL PGothic, Noto Sans CJK JP
/lib/python3.11/site-packages/matplotlib_pyodide/wasm_backend.py:44: UserWarning: Glyph 12391 (\N{HIRAGANA LETTER DE}) missing from current font.
  super().draw()
/lib/python3.11/site-packages/matplotlib_pyodide/wasm_backend.py:44: UserWarning: Glyph 26085 (\N{CJK UNIFIED IDEOGRAPH-65E5}) missing from current font.
  super().draw()
/lib/python3.11/site-packages/matplotlib_pyodide/wasm_backend.py:44: UserWarning: Glyph 26412 (\N{CJK UNIFIED IDEOGRAPH-672C}) missing from current font.
  super().draw()
/lib/python3.11/site-packages/matplotlib_pyodide/wasm_backend.py:44: UserWarning: Glyph 35486 (\N{CJK UNIFIED IDEOGRAPH-8A9E}) missing from current font.
  super().draw()

PyScript は Pure Python ライブラリで PyPI に wheel ファイルが提供されてれば import 出来るって話なんだが、
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<py-config>
packages = ["matplotlib", "japanize_matplotlib"]
</py-config>
<py-script>
import matplotlib.pyplot as plt
import japanize_matplotlib

fig, ax = plt.subplots()
ax.plot(range(10))
ax.set_title("matplotlib で日本語")
display(fig)
</py-script>
</body>
</html>
みたいにしてみると .whl が提供されてないらしく
(PY1001): Unable to install package(s) 'japanize_matplotlib'. Reason: Can't find a pure Python 3 Wheel for package(s) 'japanize_matplotlib'.See: https://pyodide.org/en/stable/usage/faq.html#micropip-can-t-find-a-pure-python-wheel for more information.
と釣れないお返事。

解法

Pyodide japanese font」でググると Pyodide 関連の情報は見当たらなかったのだが、Python 3.12 以降で matplotlib に日本語を使うって話で、以下のページを見つけた。
試しに、
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<py-config>
packages = ["matplotlib", "matplotlib-fontja"]
</py-config>
<py-script>
import matplotlib.pyplot as plt
import matplotlib_fontja

fig, ax = plt.subplots()
ax.plot(range(10))
ax.set_title("matplotlib で日本語")
display(fig)
</py-script>
</body>
</html>
としてみたところ、これは上手く行った。

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Wiki内検索

フリーエリア

管理人/副管理人のみ編集できます