全部產品
Search
文件中心

Function Compute:圖表與可視化

更新時間:Jul 22, 2026

雲沙箱 Code Interpreter 當前只支援檔案降級方案:在 Sandbox 內組建圖表檔案,再通過 Filesystem 取回。不要依賴 execution.results[].png 或 execution.results[].chart 作為正式接入路徑。

產生圖片檔案

使用 Matplotlib 組建圖表時,把圖片儲存到 Sandbox 內的固定路徑:

sandbox.run_code("""import matplotlib.pyplot as plt; plt.plot([1, 2, 3], [1, 4, 9]);plt.savefig("/tmp/chart.png")""")

TypeScript 樣本:

await sandbox.runCode(`import matplotlib.pyplot as plt; plt.plot([1, 2, 3], [1, 4, 9]); plt.savefig("/tmp/chart.png")`);

隨後讀取檔案內容:

content = sandbox.files.read("/tmp/chart.png", format="bytes")

TypeScript 樣本:

const content = await sandbox.files.read("/tmp/chart.png", { format: "bytes" });

也可以用上傳下載地址把圖片交給瀏覽器或下遊系統:

download_url = sandbox.download_url("/tmp/chart.png")

TypeScript 樣本:

const downloadUrl = await sandbox.downloadUrl("/tmp/chart.png");

產生多張圖

多張圖應使用穩定檔案名稱,避免覆蓋:

sandbox.run_code("""import matplotlib.pyplot as plt; plt.figure(); plt.plot([1, 2, 3]); plt.savefig("/tmp/chart-1.png"); plt.figure(); plt.bar(["A", "B"], [10, 20]); plt.savefig("/tmp/chart-2.png")""")

TypeScript 樣本:

await sandbox.runCode(`import matplotlib.pyplot as plt; plt.figure(); plt.plot([1, 2, 3]); plt.savefig("/tmp/chart-1.png"); plt.figure(); plt.bar(["A", "B"], [10, 20]); plt.savefig("/tmp/chart-2.png")`);

然後分別讀取或下載 /tmp/chart-1.png 和 /tmp/chart-2.png。

使用建議

  • 提示 AI 產生代碼時,要求使用 plt.savefig(),不要要求 plt.show()。

  • 約定輸出目錄和檔案名稱,例如 /tmp/chart.png 或 /tmp/charts/chart-1.png。

  • 需要長期儲存時,把圖片寫入 OSS、資料庫或業務檔案儲存體。

  • 對使用者可見的圖片,業務側應校正檔案大小和 MIME 類型。