Skip to content

Structured charts ​

For supported Matplotlib figures, Code Interpreter extracts the chart type, title, axes, and data points. Your frontend can use this structure with its own charting library.

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

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

plt.bar(["Basic", "Pro", "Enterprise"], [18, 27, 11])
plt.title("Projects by plan")
plt.show()
`);

  const chart = execution.results.find((result) => result.chart)?.chart;
  console.log(chart.type);
  console.log(chart.elements);
} finally {
  await sandbox.kill();
}

Structured output is available for line, scatter, bar, pie, and box-and-whisker charts. Check result.chart before using it: unsupported figures still provide their static image but may not have a structured representation.