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
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:
Later:
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:
Then later:
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
Related reading
- Saving the objects detected in a dataframe tensorflow object_detection
- scaling inputs data to neural network
- Scatter plot with different text at each data point
- Sci-kit learn how to print labels for confusion matrix?
- Saving meta data/information in Keras model
- Saving UTF-8 texts with json.dumps as UTF-8, not as a \u escape sequence
- scikit-learn classification on soft labels
- Scikit-learn confusion matrix
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.