Saving Plots as Images
Why saving matters
Saved plots are used in:
- Reports
- Notebooks
- Websites
- PowerPoint/Docs
Save to PNG
savefig
import matplotlib.pyplot as plt
plt.figure(figsize=(7, 4))
plt.plot([1, 2, 3], [3, 2, 5])
plt.title("Saved plot")
plt.savefig("plot.png", dpi=150, bbox_inches="tight")savefig
import matplotlib.pyplot as plt
plt.figure(figsize=(7, 4))
plt.plot([1, 2, 3], [3, 2, 5])
plt.title("Saved plot")
plt.savefig("plot.png", dpi=150, bbox_inches="tight")Notes
dpidpicontrols resolutionbbox_inches="tight"bbox_inches="tight"avoids cutting off labels
Save to PDF (for print)
Save PDF
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [3, 2, 5])
plt.title("PDF plot")
plt.savefig("plot.pdf", bbox_inches="tight")Save PDF
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [3, 2, 5])
plt.title("PDF plot")
plt.savefig("plot.pdf", bbox_inches="tight")If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
