Saving Figures

Python · Data Visualization

Theory

fig.savefig('sales.png') writes the figure to a file, and the extension picks the format. PNG is a raster image of pixels, good for slides, web pages and chat. SVG and PDF are vector formats that stay sharp at any zoom, good for reports and print. dpi (dots per inch) sets the resolution of raster output: the pixel size is figsize x dpi, so a 6 x 4 inch figure at dpi=200 is 1200 x 800 pixels.

bbox_inches='tight' trims extra white space and keeps labels from being cut off, and transparent=True removes the background. Call savefig before plt.show(), because in a script show() can leave you with an empty figure afterwards. When creating many charts in a loop, call plt.close(fig) after saving to free memory. You can also save into memory with io.BytesIO, which is how web apps send charts without writing files.

Why we use it

Charts usually end up somewhere else: a report, a slide deck, a dashboard or an email. Saving at the right size, format and resolution keeps them sharp and readable wherever they go.

When to use it

Use PNG at dpi 150-300 for slides and documents, SVG or PDF for print and scalable graphics. Use bbox_inches='tight' when labels get clipped, and close each figure after saving when you generate charts in bulk.

How it works

  1. Finished figure
  2. Will it be printed or zoomed? Yes: Save as .svg or .pdf (vector). No: Save as .png (raster).
  3. Pick dpi: pixels = figsize x dpi
  4. Add bbox_inches='tight' if labels are cut off
  5. fig.savefig(...) then plt.close(fig)
  6. Image file ready to share

Syntax

fig.savefig('chart.png', dpi=200, bbox_inches='tight')
fig.savefig('chart.svg')
fig.savefig('chart.pdf', transparent=True)
plt.close(fig)

Example

fig, ax = plt.subplots(figsize=(6, 4))
ax.bar(['Jaipur', 'Pune'], [120, 90])
fig.savefig('sales.png', dpi=200, bbox_inches='tight')

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic