Matplotlib
interactive figures
data visualization
Python plotting
save figures

How to save a Matplotlib figure, including interactive ones

Codemia Knowledge Hub ·

Use fig.savefig("plot.png") to save a Matplotlib figure. That captures how the figure looks, not its interactivity: pan, zoom, tooltips and widget state come from the live backend and are not stored in a PNG or PDF. If you need to reopen the figure in Python, pickle the figure object or, more reliably, save the data and rebuild the plot.

Saving a static image with savefig

python
1import matplotlib.pyplot as plt
2
3x = [1, 2, 3, 4]
4y = [1, 4, 9, 16]
5
6fig, ax = plt.subplots()
7ax.plot(x, y, marker="o")
8ax.set_title("Quadratic Growth")
9
10fig.savefig("plot.png", dpi=150, bbox_inches="tight")

Opening plot.png later will not restore the Matplotlib toolbar or event callbacks. That is how image formats work, not a bug.

Pickling the figure to reopen it in Python

To keep working with the figure in Python later, serialize the figure object:

python
1import pickle
2import matplotlib.pyplot as plt
3
4fig, ax = plt.subplots()
5ax.plot([1, 2, 3], [1, 8, 27])
6ax.set_title("Saved Figure Object")
7
8with open("figure.pkl", "wb") as f:
9    pickle.dump(fig, f)

Later:

python
1import pickle
2import matplotlib.pyplot as plt
3
4with open("figure.pkl", "rb") as f:
5    fig = pickle.load(f)
6
7plt.show()

This works within the same environment, but pickled figures are brittle across Matplotlib versions and Python environments, so do not treat them as a long-term archive.

Saving the data and rebuilding the plot

For long-term reproducibility, save the underlying data and plotting parameters, then regenerate the interactive figure when needed:

python
1import json
2
3plot_state = {
4    "x": [1, 2, 3, 4],
5    "y": [1, 4, 9, 16],
6    "title": "Quadratic Growth",
7}
8
9with open("plot_state.json", "w") as f:
10    json.dump(plot_state, f)

Then later:

python
1import json
2import matplotlib.pyplot as plt
3
4with open("plot_state.json") as f:
5    state = json.load(f)
6
7fig, ax = plt.subplots()
8ax.plot(state["x"], state["y"], marker="o")
9ax.set_title(state["title"])
10plt.show()

You lose the exact runtime object, but this is usually easier to maintain. Saving only the image is a common regret when you later need the data to reproduce the chart.

Interactivity in a browser or notebook

Matplotlib interactivity depends on the backend: a GUI backend in a desktop session, or a widget backend in Jupyter. Either way it is tied to a live environment, not embedded in a PNG.

For shareable browser interactivity, standard Matplotlib exports are often not enough. Options are:

  • an HTML-based export path through a compatible tool
  • a notebook environment that preserves widgets
  • a plotting library built primarily for browser interactivity

This is less about savefig and more about choosing the right delivery format for your audience. If browser-grade interactivity is the real requirement, do not force Matplotlib image exports to do a job they were not designed for.

Picking the right kind of save

Decide what "save" means in your workflow, and the implementation follows:

  • preserve appearance: use savefig
  • reopen in Python later: consider pickling, with caution
  • preserve logic reliably: save data plus plotting code
  • share interactivity in the browser: choose an HTML-capable workflow

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design