如何在 Matplotlib 生成的 PDF 中嵌入字型


要在 Matplotlib 生成的 PDF 中嵌入字型,我們可以使用 rc.Params['pdf.fonttype']=42

步驟

  • 設定圖形大小並調整子圖之間及周圍的填充。
  • 使用 figure() 方法建立新圖形或啟用現有圖形。
  • 使用 numpy 建立 xy 資料點。
  • 使用 scatter() 方法繪製 xy 資料點。
  • 設定繪圖示題。
  • 以 pdf 格式儲存圖形。

示例

import numpy as np
from matplotlib import pyplot as plt, font_manager as fm

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
plt.rcParams['pdf.fonttype'] = 42

fig, ax = plt.subplots()
x = np.random.rand(100)
y = np.random.rand(100)

ax.scatter(x, y, c=y, marker="v")

fprop = fm.FontProperties(fname='C:\Windows\Fonts\MISTRAL.TTF')

ax.set_title('Scatter Plot With Random Points',
            fontproperties=fprop, size=20, fontweight="bold")

plt.savefig("demo.pdf")

輸出

執行程式碼後,它會將下圖作為 "demo.pdf" 儲存到專案目錄中。

觀察標題的字型樣式。我們已將標題設定為 Mistral 字型

更新於:2021 年 6 月 16 日

1K+ 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.