0
0
Matplotlibdata~5 mins

DPI settings for resolution in Matplotlib

Choose your learning style9 modes available
Introduction

DPI controls how clear and sharp your plot looks when saved or displayed. Higher DPI means better quality.

When saving a plot to share in a report or presentation.
When you want a high-quality image for printing.
When displaying plots on high-resolution screens.
When you need to reduce file size by lowering DPI for quick previews.
When adjusting plot clarity for different output devices.
Syntax
Matplotlib
plt.savefig('filename.png', dpi=VALUE)

# or when creating a figure
fig = plt.figure(dpi=VALUE)

dpi stands for dots per inch and controls image resolution.

Higher dpi means more pixels and sharper images but larger file size.

Examples
Saves the plot with low resolution (50 dpi).
Matplotlib
plt.savefig('plot_low.png', dpi=50)
Saves the plot with high resolution (300 dpi), good for printing.
Matplotlib
plt.savefig('plot_high.png', dpi=300)
Creates a figure with 200 dpi resolution for display.
Matplotlib
fig = plt.figure(dpi=200)
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
Sample Program

This code creates a simple line plot and saves it twice: once with 72 dpi and once with 300 dpi. The higher dpi image will be sharper and better for printing.

Matplotlib
import matplotlib.pyplot as plt

# Create a simple plot
plt.plot([1, 2, 3], [4, 5, 6])

# Save with low dpi
plt.savefig('plot_72dpi.png', dpi=72)

# Save with high dpi
plt.savefig('plot_300dpi.png', dpi=300)

print('Saved two plots with different DPI settings.')
OutputSuccess
Important Notes

Default dpi in matplotlib is usually 100.

Changing dpi affects only saved images or figure size, not the data itself.

Use dpi to balance image quality and file size.

Summary

DPI controls the sharpness of saved or displayed plots.

Higher DPI means better quality but larger files.

Use plt.savefig() with dpi to set resolution.