MatplotlibXBy @CodeWithMunnaX
Catalog
Track: 3D & Projects
Production & Export10 mins

48. High-Resolution Exporting (savefig, PDF, SVG, DPI)

`plt.savefig()` saves figures directly to disk in raster formats (PNG, JPG) or infinitely scalable vector formats (PDF, SVG, EPS) suitable for academic journals and LaTeX theses.

Mental Model Intuition

Exporting a high-res master render from Photoshop or Adobe Illustrator.

Core Concepts & Mechanics

`bbox_inches='tight'`Cropping

Automatically calculates the exact bounding box enclosing all labels, titles, and legends, cropping excess white border margins.

Always pass `bbox_inches='tight'` to prevent labels on the edges from being clipped in exported files.
plt.savefig('plot.png', dpi=300, bbox_inches='tight')
`transparent=True`Transparency

Renders the figure background transparent (alpha=0), allowing the chart to blend seamlessly into dark mode slides or web apps.

Use `transparent=True` when preparing charts for Keynote/PowerPoint presentations.
plt.savefig('chart.png', transparent=True)

Common Beginner Mistakes & Pro Fixes

Avoid the top errors and bad plotting practices that trip up new Python developers

Troubleshooting

Quick Lesson Takeaways

  • `plt.savefig('name.png', dpi=300)` saves high-resolution raster files.
  • `bbox_inches='tight'` crops whitespace borders cleanly.
  • Export as `.svg` or `.pdf` for infinite resolution vector scaling.
  • Always call `savefig()` before `show()`.
Topic 48 • Python IDE
Python 3.12 • Matplotlib
Run Python code to inspect terminal output.

Real-World Datasets (1-Click Switcher)

Switch from abstract numbers to relatable Cricket, Cinema & Business charts

5 Curated Datasets

Hands-on Challenge: Simulate Savefig

+50 XP

Write Matplotlib code to solve the objective and see your live plot render

Objective Instructions:

Print the correct high-res savefig command using `print('plt.savefig("chart.png", dpi=300, bbox_inches="tight")')`.

Required Keywords:✓ savefig✓ dpi=300
Challenge Code Editor
Challenge Output GraphLive Vector Preview
Write code on the left to render live challenge plot
End of Lesson Knowledge Check

Quick Mini Quiz: 2 Concept Questions

Test your understanding to unlock the lesson completion badge

Q1

Which Matplotlib function is used to create a standard continuous 2D line plot?

Q2

What must you call after specifying label='...' so that the legend actually appears on the canvas?

0 of 2 answered