我正在嘗試使用 matplotlib 使用谷歌云函式繪制圖形,但我需要使用字體 Balthazar。
這是我正在使用的簡單示例https://matplotlib.org/stable/gallery/shapes_and_collections/scatter.html
這是字體。https://www.1001freefonts.com/balthazar.font
我可以在我的桌面上使用:
hfont = {'fontname': 'Balthazar'}
plt.text(6, 0.5, 'Test', **hfont, fontsize=20)
因為安裝了字體。如何在云函式中使用這種字體?
uj5u.com熱心網友回復:
您可以在 Cloud Function 的 matplotlib 中使用自定義字體,而無需安裝字體。當您部署 Cloud Functions 時,它將上傳您的函式目錄的內容。您可以使用此方法更改字體而無需安裝。以下是步驟:
- 下載您的自定義字體檔案(解壓縮檔案)。
- 將您的字體檔案放在您的函式根目錄中,例如:
function/Balthazar-Regular.ttf
或為字體檔案創建另一個檔案夾
function/font_folder/Balthazar-Regular.ttf
- 示例代碼:
# font file directory
font_dir = ['/font_folder']
# Add every font at the specified location
font_files = font_manager.findSystemFonts(fontpaths=font_dir)
for font_file in font_files:
font_manager.fontManager.addfont(font_file)
# Set font family globally
plt.rcParams['font.family'] = 'Balthazar'
# The details below is the sample code in matplotlib
np.random.seed(19680801)
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = (30 * np.random.rand(N))**2 # 0 to 15 point radii
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()
- 部署您的云功能。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/317799.html
下一篇:影片多個影像系列的子圖
