在 Matplotlib 中將繪圖儲存為 SVG 檔案
Maxim Maeder
2023年1月30日

本教程教你將圖形儲存為可縮放向量圖形或 SVG。
在 Matplotlib 中生成資料以建立繪圖
在使用繪圖之前,我們需要設定我們的指令碼以使用該庫。所以我們從匯入 Matplotlib 開始。
此外,我們從 random
模組載入 randrange
函式以快速生成一些資料。請記住,它對你來說看起來會有所不同。
import matplotlib.pyplot as plt
from random import randrange
data_1 = [randrange(0, 10) for _ in range(0, 10)]
plt.plot(data_1)
在 Matplotlib 中使用 savefig
函式將繪圖儲存為 SVG 檔案
將繪圖儲存為 SVG 檔案很容易。只需按以下方式呼叫 savefig
函式。
plt.savefig("plot.svg", format="svg")
執行程式碼後,圖形會儲存到當前目錄,可伸縮。
完整程式碼:
import matplotlib.pyplot as plt
from random import randrange
data_1 = [randrange(0, 10) for _ in range(0, 10)]
plt.plot(data_1)
plt.savefig("plot.svg", format="svg")
作者: Maxim Maeder
Hi, my name is Maxim Maeder, I am a young programming enthusiast looking to have fun coding and teaching you some things about programming.
GitHub