0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Snowflakeでmatplotlibのグラフに日本語フォントを表示させたい

Posted at

1行まとめ

Snowflake Managed Stages にフォントファイルを置いて、それを呼び出す。

詳細

  1. 日本語フォントをダウンロードしてくる
    https://moji.or.jp/ipafont/ipaex00401/
    このへんとかから。zipは解凍しておく。

  2. Notebook/Streamlit を作成するデータベース.スキーマに、Snowflake Managed Stagesを作成し、1. のフォントファイルを配置する

  3. Notebook/streamlit を作成する
    3-1. 右上のPackages から matplotlib を追加する
    image.png

3-2. コードを書く

sample_notebook.py
from io import BytesIO
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
from snowflake.snowpark.context import get_active_session

session = get_active_session()

# フォントファイルのパス 
path = "@HOGE_STG/ipaexg.ttf"

# ファイルの読み込み
with session.file.get_stream(path, decompress=False) as f:
    s = f.read()

# BytesIO: メモリ上でバイナリデータを扱うための機能
with BytesIO() as bs:
    bs.write(s)
    content = bs.getvalue()

# SiSの仮フォルダ領域への書き込み
with open(f"/tmp/ipaexg.ttf", "wb") as s:
    s.write(content)

font_prop = fm.FontProperties(fname="/tmp/ipaexg.ttf")
fm.fontManager.addfont("/tmp/ipaexg.ttf")
plt.rcParams['font.family'] = font_prop.get_name()

# グラフを描画
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], [10, 20, 25, 30])
ax.set_title('サンプルグラフ')

plt.show()

3-3. 実行する
image.png

もうちょっとかわいらしいフォントでやってみる

image.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?