Lesson 21
Saving Figures
Save a finished chart with savefig as PNG, SVG, or PDF, set its size and resolution, trim the margins, and close figures you no longer need.
A chart on screen is only half the job. Reports, slides, and web pages need a file. savefig writes the current figure to disk, and the file extension you choose decides the format.
%7D42%25%2C100%25%7Btransform%3AscaleY(1)%7D%7D.mdfx-wide%7Banimation%3AmdfxWide%207s%20ease-out%20infinite%3Btransform-box%3Afill-box%3Btransform-origin%3Aleft%7D%40keyframes%20mdfxWide%7B0%25%2C6%25%7Btransform%3AscaleX(0)%7D42%25%2C100%25%7Btransform%3AscaleX(1)%7D%7D.mdfx-pop%7Banimation%3AmdfxPop%207s%20ease-out%20infinite%3Btransform-box%3Afill-box%3Btransform-origin%3Acenter%7D%40keyframes%20mdfxPop%7B0%25%2C6%25%7Btransform%3Ascale(0)%7D34%25%2C100%25%7Btransform%3Ascale(1)%7D%7D%40media%20(prefers-reduced-motion%3Areduce)%7B.mdfx-an%2C.mdfx-dash%2C.mdfx-grow%2C.mdfx-wide%2C.mdfx-pop%7Banimation%3Anone%7D.mdfx-dash%7Bstroke-dashoffset%3A0%7D%7D%3C%2Fstyle%3E%3Cdefs%3E%3ClinearGradient%20id%3D'g'%20x1%3D'0'%20y1%3D'0'%20x2%3D'1'%20y2%3D'1'%3E%3Cstop%20offset%3D'0%25'%20stop-color%3D'%237C3AED'%2F%3E%3Cstop%20offset%3D'100%25'%20stop-color%3D'%232563EB'%2F%3E%3C%2FlinearGradient%3E%3C%2Fdefs%3E%3Crect%20width%3D'640'%20height%3D'360'%20rx%3D'28'%20fill%3D'%230F172A'%2F%3E%3Crect%20x%3D'28'%20y%3D'28'%20width%3D'584'%20height%3D'304'%20rx%3D'22'%20fill%3D'none'%20stroke%3D'url(%23g)'%20stroke-width%3D'3'%2F%3E%3Crect%20x%3D'48'%20y%3D'48'%20width%3D'544'%20height%3D'48'%20rx%3D'14'%20fill%3D'%231E293B'%2F%3E%3Crect%20class%3D'mdfx-an'%20x%3D'48'%20y%3D'48'%20width%3D'6'%20height%3D'48'%20rx%3D'3'%20fill%3D'url(%23g)'%20opacity%3D'1'%20style%3D'animation-delay%3A0.00s'%2F%3E%3Ctext%20x%3D'68'%20y%3D'69'%20fill%3D'%23F8FAFC'%20font-size%3D'15'%20font-family%3D'ui-monospace%2Cmonospace'%3Eplt.figure(figsize%3D(8%2C%204))%3C%2Ftext%3E%3Ctext%20x%3D'68'%20y%3D'87'%20fill%3D'%23CBD5E1'%20font-size%3D'13'%20font-family%3D'ui-sans-serif%2Csystem-ui'%3Esize%20in%20inches%2C%20set%20before%20drawing%3C%2Ftext%3E%3Crect%20x%3D'48'%20y%3D'106'%20width%3D'544'%20height%3D'48'%20rx%3D'14'%20fill%3D'%231E293B'%2F%3E%3Crect%20class%3D'mdfx-an'%20x%3D'48'%20y%3D'106'%20width%3D'6'%20height%3D'48'%20rx%3D'3'%20fill%3D'url(%23g)'%20opacity%3D'1'%20style%3D'animation-delay%3A0.50s'%2F%3E%3Ctext%20x%3D'68'%20y%3D'127'%20fill%3D'%23F8FAFC'%20font-size%3D'15'%20font-family%3D'ui-monospace%2Cmonospace'%3Eplt.plot(...)%20and%20labels%3C%2Ftext%3E%3Ctext%20x%3D'68'%20y%3D'145'%20fill%3D'%23CBD5E1'%20font-size%3D'13'%20font-family%3D'ui-sans-serif%2Csystem-ui'%3Ebuild%20the%20chart%20as%20usual%3C%2Ftext%3E%3Crect%20x%3D'48'%20y%3D'164'%20width%3D'544'%20height%3D'48'%20rx%3D'14'%20fill%3D'%231E293B'%2F%3E%3Crect%20class%3D'mdfx-an'%20x%3D'48'%20y%3D'164'%20width%3D'6'%20height%3D'48'%20rx%3D'3'%20fill%3D'url(%23g)'%20opacity%3D'1'%20style%3D'animation-delay%3A1.00s'%2F%3E%3Ctext%20x%3D'68'%20y%3D'185'%20fill%3D'%23F8FAFC'%20font-size%3D'15'%20font-family%3D'ui-monospace%2Cmonospace'%3Eplt.savefig(%22chart.png%22%2C%20dpi%3D150)%3C%2Ftext%3E%3Ctext%20x%3D'68'%20y%3D'203'%20fill%3D'%23CBD5E1'%20font-size%3D'13'%20font-family%3D'ui-sans-serif%2Csystem-ui'%3Ewrite%20the%20file%3B%20format%20from%20the%20name%3C%2Ftext%3E%3Crect%20x%3D'48'%20y%3D'222'%20width%3D'544'%20height%3D'48'%20rx%3D'14'%20fill%3D'%231E293B'%2F%3E%3Crect%20class%3D'mdfx-an'%20x%3D'48'%20y%3D'222'%20width%3D'6'%20height%3D'48'%20rx%3D'3'%20fill%3D'url(%23g)'%20opacity%3D'1'%20style%3D'animation-delay%3A1.50s'%2F%3E%3Ctext%20x%3D'68'%20y%3D'243'%20fill%3D'%23F8FAFC'%20font-size%3D'15'%20font-family%3D'ui-monospace%2Cmonospace'%3Ebbox_inches%3D'tight%22%3C%2Ftext%3E%3Ctext%20x%3D'68'%20y%3D'261'%20fill%3D'%23CBD5E1'%20font-size%3D'13'%20font-family%3D'ui-sans-serif%2Csystem-ui'%3Etrim%20the%20empty%20margin%3C%2Ftext%3E%3Crect%20x%3D'48'%20y%3D'280'%20width%3D'544'%20height%3D'48'%20rx%3D'14'%20fill%3D'%231E293B'%2F%3E%3Crect%20class%3D'mdfx-an'%20x%3D'48'%20y%3D'280'%20width%3D'6'%20height%3D'48'%20rx%3D'3'%20fill%3D'url(%23g)'%20opacity%3D'1'%20style%3D'animation-delay%3A2.00s'%2F%3E%3Ctext%20x%3D'68'%20y%3D'301'%20fill%3D'%23F8FAFC'%20font-size%3D'15'%20font-family%3D'ui-monospace%2Cmonospace'%3Eplt.close()%3C%2Ftext%3E%3Ctext%20x%3D'68'%20y%3D'319'%20fill%3D'%23CBD5E1'%20font-size%3D'13'%20font-family%3D'ui-sans-serif%2Csystem-ui'%3Efree%20the%20figure%20once%20it%20is%20saved%3C%2Ftext%3E%3C%2Fsvg%3E)
'%20stroke-width%3D'3'%2F%3E%3Crect%20x%3D'48'%20y%3D'48'%20width%3D'544'%20height%3D'48'%20rx%3D'14'%20fill%3D'%231E293B'%2F%3E%3Crect%20class%3D'mdfx-an'%20x%3D'48'%20y%3D'48'%20width%3D'6'%20height%3D'48'%20rx%3D'3'%20fill%3D'url(%23g)'%20opacity%3D'1'%20style%3D'animation-delay%3A0.00s'%2F%3E%3Ctext%20x%3D'68'%20y%3D'69'%20fill%3D'%23F8FAFC'%20font-size%3D'15'%20font-family%3D'ui-monospace%2Cmonospace'%3Eplt.figure(figsize%3D(8%2C%204))%3C%2Ftext%3E%3Ctext%20x%3D'68'%20y%3D'87'%20fill%3D'%23CBD5E1'%20font-size%3D'13'%20font-family%3D'ui-sans-serif%2Csystem-ui'%3Esize%20in%20inches%2C%20set%20before%20drawing%3C%2Ftext%3E%3Crect%20x%3D'48'%20y%3D'106'%20width%3D'544'%20height%3D'48'%20rx%3D'14'%20fill%3D'%231E293B'%2F%3E%3Crect%20class%3D'mdfx-an'%20x%3D'48'%20y%3D'106'%20width%3D'6'%20height%3D'48'%20rx%3D'3'%20fill%3D'url(%23g)'%20opacity%3D'1'%20style%3D'animation-delay%3A0.50s'%2F%3E%3Ctext%20x%3D'68'%20y%3D'127'%20fill%3D'%23F8FAFC'%20font-size%3D'15'%20font-family%3D'ui-monospace%2Cmonospace'%3Eplt.plot(...)%20and%20labels%3C%2Ftext%3E%3Ctext%20x%3D'68'%20y%3D'145'%20fill%3D'%23CBD5E1'%20font-size%3D'13'%20font-family%3D'ui-sans-serif%2Csystem-ui'%3Ebuild%20the%20chart%20as%20usual%3C%2Ftext%3E%3Crect%20x%3D'48'%20y%3D'164'%20width%3D'544'%20height%3D'48'%20rx%3D'14'%20fill%3D'%231E293B'%2F%3E%3Crect%20class%3D'mdfx-an'%20x%3D'48'%20y%3D'164'%20width%3D'6'%20height%3D'48'%20rx%3D'3'%20fill%3D'url(%23g)'%20opacity%3D'1'%20style%3D'animation-delay%3A1.00s'%2F%3E%3Ctext%20x%3D'68'%20y%3D'185'%20fill%3D'%23F8FAFC'%20font-size%3D'15'%20font-family%3D'ui-monospace%2Cmonospace'%3Eplt.savefig(%22chart.png%22%2C%20dpi%3D150)%3C%2Ftext%3E%3Ctext%20x%3D'68'%20y%3D'203'%20fill%3D'%23CBD5E1'%20font-size%3D'13'%20font-family%3D'ui-sans-serif%2Csystem-ui'%3Ewrite%20the%20file%3B%20format%20from%20the%20name%3C%2Ftext%3E%3Crect%20x%3D'48'%20y%3D'222'%20width%3D'544'%20height%3D'48'%20rx%3D'14'%20fill%3D'%231E293B'%2F%3E%3Crect%20class%3D'mdfx-an'%20x%3D'48'%20y%3D'222'%20width%3D'6'%20height%3D'48'%20rx%3D'3'%20fill%3D'url(%23g)'%20opacity%3D'1'%20style%3D'animation-delay%3A1.50s'%2F%3E%3Ctext%20x%3D'68'%20y%3D'243'%20fill%3D'%23F8FAFC'%20font-size%3D'15'%20font-family%3D'ui-monospace%2Cmonospace'%3Ebbox_inches%3D'tight%22%3C%2Ftext%3E%3Ctext%20x%3D'68'%20y%3D'261'%20fill%3D'%23CBD5E1'%20font-size%3D'13'%20font-family%3D'ui-sans-serif%2Csystem-ui'%3Etrim%20the%20empty%20margin%3C%2Ftext%3E%3Crect%20x%3D'48'%20y%3D'280'%20width%3D'544'%20height%3D'48'%20rx%3D'14'%20fill%3D'%231E293B'%2F%3E%3Crect%20class%3D'mdfx-an'%20x%3D'48'%20y%3D'280'%20width%3D'6'%20height%3D'48'%20rx%3D'3'%20fill%3D'url(%23g)'%20opacity%3D'1'%20style%3D'animation-delay%3A2.00s'%2F%3E%3Ctext%20x%3D'68'%20y%3D'301'%20fill%3D'%23F8FAFC'%20font-size%3D'15'%20font-family%3D'ui-monospace%2Cmonospace'%3Eplt.close()%3C%2Ftext%3E%3Ctext%20x%3D'68'%20y%3D'319'%20fill%3D'%23CBD5E1'%20font-size%3D'13'%20font-family%3D'ui-sans-serif%2Csystem-ui'%3Efree%20the%20figure%20once%20it%20is%20saved%3C%2Ftext%3E%3C%2Fsvg%3E)
Save the current figure
Call savefig with a file name. Call it before show, because once a window is closed the figure may be gone and the saved file comes out blank.
import os
import matplotlib.pyplot as plt
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
signups = [42, 58, 51, 66, 73, 61, 47]
plt.plot(days, signups, marker="o")
plt.title("Signups this week")
plt.savefig("signups.png")
plt.show()
print(os.path.exists("signups.png"))Output:
TrueThe extension picks the format
.png is a raster image, good for the web and slides. .svg and .pdf are vector formats that stay sharp at any zoom, which is what print and publications want. You can also pass format= explicitly.
import os
import matplotlib.pyplot as plt
plt.bar(["Python", "NumPy", "pandas"], [120, 86, 94], color="#7C3AED")
plt.title("Enrolments")
for name in ["tracks.png", "tracks.svg", "tracks.pdf"]:
plt.savefig(name)
plt.close()
for name in ["tracks.png", "tracks.svg", "tracks.pdf"]:
print(name, os.path.getsize(name) > 0)Output:
tracks.png True
tracks.svg True
tracks.pdf TrueSize and resolution
figsize is width and height in inches, set when the figure is made. dpi is dots per inch, set when it is saved. Pixels are the two multiplied, so an 8 by 4 inch figure at 150 dpi is 1200 by 600 pixels.
import matplotlib.pyplot as plt
from matplotlib.image import imread
plt.figure(figsize=(8, 4))
plt.plot([1, 2, 3, 4], [3, 5, 4, 6])
plt.savefig("wide.png", dpi=150)
plt.close()
height, width = imread("wide.png").shape[:2]
print(width, "x", height)Output:
1200 x 600Trim the margins
bbox_inches="tight" shrinks the saved area to fit the chart and its labels, so long titles are not clipped and there is no wide white border. transparent=True drops the white background for use on coloured slides.
import matplotlib.pyplot as plt
from matplotlib.image import imread
plt.figure(figsize=(6, 3))
plt.plot([1, 2, 3], [2, 4, 3])
plt.title("A long title that would sit close to the edge")
plt.savefig("loose.png", dpi=100)
plt.savefig("tight.png", dpi=100, bbox_inches="tight", transparent=True)
plt.close()
print("loose:", imread("loose.png").shape[:2])
print("tight:", imread("tight.png").shape[:2])Output:
loose: (300, 600)
tight: (297, 516)Close what you have saved
Every plt.figure stays in memory until it is closed. In a loop that saves many charts, call plt.close() after each save, or the program slowly fills up. plt.get_fignums() lists the figures still open.
import matplotlib.pyplot as plt
weeks = {"week 1": [3, 5, 4], "week 2": [4, 6, 5], "week 3": [5, 5, 7]}
for m, p in weeks.items():
plt.figure()
plt.plot(p)
plt.title(m)
plt.savefig(m.replace(" ", "_") + ".png")
plt.close()
print("open figures:", plt.get_fignums())Output:
open figures: []In this page's playground, files are written to a temporary folder in your browser's memory, so they disappear when you reload. On your own computer they land next to your script.
Key takeaways
-
savefig writes the current figure, so call it before show
-
The extension picks the format: png for screens, svg or pdf for print
-
figsize is inches, dpi is pixels per inch, and pixels are the product
-
bbox_inches="tight" trims margins; plt.close() frees saved figures
-
Next: What Is Seaborn
-
Previous: Styling a Pie Chart
Loading playground…
Check your understanding
Saving Figures Quiz
Which function writes a chart to a file?
1 / 10