Lesson 13 · Data visualisation in python
How to Save and Export High-Quality Matplotlib Plots for Publication and Presentation
Welcome! Today, we will learn how to make beautiful charts with Matplotlib and save them to your computer. No experience needed. We will use easy examples,…
- CourseData visualisation in python
- Lesson13 of 34
- Video15 min
- FormatJupyter notebook · 16 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbSaving and Exporting High-Quality Plots in Matplotlib#
Welcome! Today, we will learn how to make beautiful charts with Matplotlib and save them to your computer.
No experience needed. We will use easy examples, add real data, and help you share your work like a pro!
import warnings
warnings.filterwarnings("ignore")
# We turn off warnings so your notebook stays clean.
Why Save Your Plots?#
Saving plots means you can use them in reports, emails, or share them with friends.
We will learn to save in different formats, like PNG and PDF. These look nice when printed or used online.
# First steps with matplotlib
import matplotlib.pyplot as plt
# Let us make a simple line plot for practice.
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.title("My First Plot")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.show()
# Data setup
import pandas as pd
url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/daily-min-temperatures.csv"
data = pd.read_csv(url)
print("Data shape:", data.shape)
display(data.head())
# First look: plot the temperature time series
plt.figure(figsize=(10,4))
plt.plot(data["Date"], data["Temp"], color="royalblue")
plt.title("Daily Minimum Temperature (Melbourne, Australia)")
plt.xlabel("Date")
plt.ylabel("Temperature (Celsius)")
plt.tight_layout()
plt.show()
How Are Plots Saved?#
Matplotlib can save plots to your computer.
You will learn to choose where it goes, the format (like PNG or PDF), and what size.
# Save a plot as PNG
plt.figure(figsize=(8,4))
plt.plot(data["Date"][:60], data["Temp"][:60], color="tomato")
plt.title("First 60 Days of Temperature")
plt.xlabel("Date")
plt.ylabel("Temperature (Celsius)")
plt.tight_layout()
plt.savefig("first_60_days.png")
plt.close()
# Save a plot as PDF (good for print)
plt.figure(figsize=(8,4))
plt.plot(data["Date"][120:180], data["Temp"][120:180], color="seagreen", marker="o")
plt.title("Days 121-180: Temperature")
plt.xlabel("Date")
plt.ylabel("Temperature (Celsius)")
plt.tight_layout()
plt.savefig("days_121_180.pdf")
plt.close()
# Increase image quality (dpi= dots per inch)
plt.figure(figsize=(10, 5))
plt.plot(data["Date"][:365], data["Temp"][:365], lw=2, color="mediumpurple")
plt.title("Year Overview (1981)")
plt.xlabel("Date")
plt.ylabel("Temperature (Celsius)")
plt.tight_layout()
plt.savefig("year_overview_highres.png", dpi=300)
plt.close()
Comparing Image Formats#
- PNG: Best for screens, like slides and web pages. Sharp and supports transparency.
- JPG: Smaller files, but blurry for chartsuse PNG instead for line work.
- PDF/SVG: Never blurry. Zoom as much as you like. Great for printing and vector work.
# Save with a transparent background for slides
plt.figure(figsize=(8, 4))
plt.plot(data["Date"][:30], data["Temp"][:30], color="goldenrod", linewidth=3)
plt.title("First Month: Transparent Chart")
plt.xlabel("Date")
plt.ylabel("Temperature (Celsius)")
plt.tight_layout()
plt.savefig("first_month_transparent.png", transparent=True)
plt.close()
# Save specific figure only, not all plots
fig, ax = plt.subplots(figsize=(6,3))
ax.plot(data["Date"][20:40], data["Temp"][20:40], color="slateblue", marker="s")
ax.set_title("Dates 21-40")
ax.set_xlabel("Date")
ax.set_ylabel("Temperature (Celsius)")
fig.tight_layout()
fig.savefig("dates_21_40_custom.png")
plt.close(fig) # Close only this plot, in case we have others.
# Add extra quality options: pad_inches, bbox_inches
plt.figure(figsize=(8,3))
plt.plot(data["Date"][10:60], data["Temp"][10:60], color="crimson")
plt.title("Zoomed In: Days 11-60")
plt.xlabel("Date", fontsize=12)
plt.ylabel("Temperature (Celsius)", fontsize=12)
plt.tight_layout()
plt.savefig("zoomed_in_cropped.png", bbox_inches="tight", pad_inches=0.2)
plt.close()
Common Errors When Saving Plots#
- Forgetting to run
plt.savefig()beforeplt.show()can leave you with a blank file. Always save before showing! - Misspelling your filename or extension, so you cannot find your image later.
- Overwriting an old file by using the same name twiceuse unique names.
# Example of incorrect order
plt.figure()
plt.plot([1,2,3], [3,5,2])
plt.show()
plt.savefig("oops.png") # This will not save your chart correctly!
# Accept file names from user
filename = input("What do you want to call your image file? (e.g. my_plot.png): ")
plt.figure(figsize=(6,3))
plt.plot(data["Date"][50:100], data["Temp"][50:100], color="navy")
plt.title("Days 51-100")
plt.tight_layout()
plt.savefig(filename)
plt.close()
# Save multiple plots easily in a loop
for i in range(3):
plt.figure(figsize=(5,2))
plt.plot(data["Date"][i*30:(i+1)*30], data["Temp"][i*30:(i+1)*30])
plt.title(f"Month {i+1}")
plt.tight_layout()
fname = f"month_{i+1}.png"
plt.savefig(fname, dpi=200)
plt.close()
Mini-Project: Export a Special Plot#
Let us pull out the coldest week of the year and save it with a title and grid lines.
This is useful if you want to highlight exciting data for reports or schoolwork!
# Find the coldest week
coldest = data.nsmallest(7, "Temp")
plt.figure(figsize=(7,3))
plt.plot(coldest["Date"], coldest["Temp"], marker="o", linestyle="-", color="skyblue")
plt.title("Coldest 7 Days")
plt.xlabel("Date")
plt.ylabel("Temperature (Celsius)")
plt.grid(True, linestyle=":")
plt.tight_layout()
plt.savefig("coldest_week.png", dpi=250)
plt.close()
# Best practices: always set size, dpi, and save before plt.show()
plt.figure(figsize=(8,4), dpi=200)
plt.plot(data["Date"][300:330], data["Temp"][300:330], color="teal", marker="D")
plt.title("30 Days - Good Practice Example")
plt.xlabel("Date")
plt.ylabel("Temperature (Celsius)")
plt.tight_layout()
plt.savefig("good_practice_plot.png")
plt.show()
Troubleshooting: Where Did My File Go?#
If you cannot find the file:
- Double check if you used './' to save in the current folder.
- Path problems happen if you put folders in your name, and they do not exist.
- Try just a simple name first, like 'test.png'.
- Reload your Jupyter file browser to see new files if needed.
# Quick tip: Save in SVG for web or graphic design
plt.figure(figsize=(7,3))
plt.plot(data["Date"][200:240], data["Temp"][200:240], color="forestgreen")
plt.title("SVG Example Plot")
plt.xlabel("Date")
plt.ylabel("Temperature (Celsius)")
plt.tight_layout()
plt.savefig("svg_example.svg")
plt.close()
Now You Try! Challenge#
Pick any 7 days from the dataset. Make a plot showing only those 7 days. Save it as a PNG and a PDF. Try a new color and a bold title.
Practice helps you remember!
Recap: Expert Plots, Easy Steps#
- Save charts in many formats (PNG, PDF, SVG)
- Always set size and dpi for top quality
- Choose the right format for your needs
- Save before showing the plot in code
- Use unique names so you do not lose work
You can now export and share your results!
Thanks for Learning with Us!#
If this was helpful, please like and subscribe to our YouTube channel.
Happy plotting! See you in the next tutorial!
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



