Saving and exporting
Last updated
A chart in a notebook is only half done: it still has to reach a report, a slide or a page, at the right size, looking the same as every other chart, and readable by someone who can't see it. In this lesson you'll save charts as SVG, PNG and PDF, choose a size and resolution for print, write a house style once and use it everywhere, and write alt text.
By the end of this lesson you can
- Save a figure with savefig, and choose between SVG, PDF and PNG
- Work out a PNG's pixels from its size and DPI, and pick a DPI for print
- Write a style sheet so every chart in a project matches
- Write alt text that gives a chart's finding, not just its type
savefig and formats
fig.savefig(name) writes the figure to a file, in the format the name ends with. Run the cells.
The first cell draws a 4 × 2.5 inch chart and saves it three times; plt.close(fig) then closes it without showing it, as a script would. The second cell opens the two PNGs and reads their sizes in pixels with Pillow, the imaging library matplotlib itself uses.
Two kinds of format:
- Vector: SVG and PDF. The file holds shapes and text, not pixels, so it's sharp at any size and its text can be searched and selected. Use SVG for web pages and PDF for print and LaTeX. This course's own charts are SVG.
- Raster: PNG. The file holds a grid of pixels. Use it where vector files aren't accepted (some slide and chat apps), and for charts with so many points that a vector file gets huge. Never JPEG for charts: it blurs sharp edges and text.
A PNG's size in pixels is its size in inches times its DPI, dots per inch:
4 inches at 72 DPI is 288 pixels; at 300 DPI, 1,200. That's what the second cell printed: (288, 180) and (1200, 750).
Wrong way: a 72 DPI chart in print
The 72 DPI file looks fine on screen, so it goes into the report. Then the report is printed, and the chart is stretched across the page. Choose each version and compare.
288 × 180 pixels, stretched to fill the width below.
At 72 DPI, 288 pixels are stretched over the page's width: soft edges, fuzzy letters. At 150 it's acceptable; at 300, the usual standard for print, it's crisp. The SVG has no pixels to run out of, so it's crisp at any size.
The rule of thumb: decide the size the chart will be shown at first, and set figsize to that, in inches. Then save as SVG or PDF, or as PNG at 300 DPI for print and 150 to 200 for screens. Don't make a small chart and scale it up later: its text scales too, and the file's pixels run out. The check: before a chart goes into a report, open the file at the size it'll be printed.
A house style
Every chart in a report should look like it belongs with the others: the same fonts, colours, spines and grid. Instead of repeating the same ten lines in every cell, put them in a style sheet, a small text file of settings, and use it once at the top of the notebook. Run the cells.
house.mplstyle(printed by the first cell) is one setting per line,name: value. The names are matplotlib's rcParams, its list of defaults:axes.spines.top: Falsehides every top spine,axes.titlelocation: leftputs every title on the left,axes.prop_cyclesets the colours, grey then pink.savefig.bbox: tighttrims empty margins when saving.plt.style.use("house.mplstyle")applies it to every chart drawn afterwards. The chart code itself stays short.plt.style.use("default")goes back to matplotlib's defaults, for comparison. matplotlib also comes with ready-made styles;plt.style.availablelists them.
Keep the style file in the project, next to src/ (lesson 1.5), so every notebook and script uses the same one.
Alt text for every chart
A chart is a picture. Someone using a screen reader, or reading with images turned off, gets only its alt text, the short description that goes with the image. In Markdown it goes in the square brackets: . In HTML it's the alt attribute.
Bad alt text says what kind of picture it is: "Bar chart." Good alt text says what the sighted reader would take away:
- What it is, briefly: a bar chart of average kilos sold each day of the week.
- The finding, the same as the title: Saturday is the busiest day.
- The key numbers that back it up: about 227 kg on Saturdays, against 149 to 191 kg on other days.
Keep it to a sentence or two. If the reader needs every number, put a table under the chart too, which helps everyone. Write alt text for this chart in the Markdown cell, then press Show it.
One answer: "Bar chart of the average kilos sold each day of the week across the four stores. Saturday is the busiest day, at about 227 kg; the other days are between 149 kg (Tuesday) and 191 kg (Friday)."
The notebooks in this course show your charts with their titles as alt text, which is better than nothing; in a report, write the real thing.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- matplotlib: matplotlib.pyplot.savefig: formats, dpi and bbox_inches
- matplotlib: Customizing Matplotlib with style sheets and rcParams: the .mplstyle format and every setting it can hold
- matplotlib: Style sheets reference: the styles that come with matplotlib
- W3C Web Accessibility Initiative: Complex images: short alt text plus a longer description for charts
- MDN: The Image Embed element, alt: what alt text is for