Skip to content

Static charts

When Python displays a Matplotlib figure, Code Interpreter adds its encoded image to the execution results. Find the PNG result, decode it, and save it in your application:

mjs
const sandbox = await Sandbox.create("code-interpreter");

try {
  const execution = await sandbox.runCode(`
import matplotlib.pyplot as plt

plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.title("Growth")
plt.show()
`);

  const image = execution.results.find((result) => result.png);
  await writeFile("growth.png", Buffer.from(image.png, "base64"));
  console.log("Saved growth.png");
} finally {
  await sandbox.kill();
}

The result may expose several formats at once. Use result.formats() to inspect the formats available for a particular figure.