從 Matplotlib 影像匯出 svg 檔案


若要從 matplotlib 影像匯出 SVG 檔案,我們可以執行以下步驟:

  • 設定影像大小並調整子圖之間的邊距和周圍的邊距。

  • 建立一個影像和一組子圖。

  • 使用 numpy 建立隨機的 x 和 y 資料點。

  • 使用 plot() 方法繪製 x 和 y 資料點。

  • 使用 savefig() 方法儲存 .svg 格式檔案。

範例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots()
x = np.random.rand(10)
y = np.random.rand(10)
ax.plot(x, y, ls='dotted', linewidth=2, color='red')
plt.savefig("myimg.svg")

輸出

當執行此程式碼時,它將建立一個名為 "myimg.svg" 的 SVG 檔案,並將其儲存到當前目錄中。

更新於: 12-5-2021

12K+ 瀏覽量

開啟您的 職業生涯

完成課程獲得認證

立刻開始
廣告
© . All rights reserved.