0
0
Matplotlibdata~5 mins

Figure size and DPI in Matplotlib

Choose your learning style9 modes available
Introduction

Figure size and DPI control how big and clear your plot looks. This helps make your charts easy to see and share.

When you want to make a plot bigger or smaller to fit a report or presentation.
When you need a high-quality image for printing or publishing.
When you want to control the resolution of your saved plot image.
When you want consistent plot sizes across multiple charts.
When you want to adjust the plot size to fit on a screen or paper.
Syntax
Matplotlib
plt.figure(figsize=(width, height), dpi=dpi_value)

figsize sets the width and height in inches.

dpi means dots per inch, controlling image resolution.

Examples
Creates a figure 6 inches wide and 4 inches tall with default DPI (usually 100).
Matplotlib
plt.figure(figsize=(6, 4))
Creates a larger figure with higher resolution for clearer details.
Matplotlib
plt.figure(figsize=(8, 6), dpi=150)
Uses default size but increases resolution for sharper image.
Matplotlib
plt.figure(dpi=200)
Sample Program

This code shows two plots with the same size but different DPI. The second plot will look sharper because it has more dots per inch.

Matplotlib
import matplotlib.pyplot as plt

# Create a figure 5x3 inches with 100 dpi
plt.figure(figsize=(5, 3), dpi=100)
plt.plot([1, 2, 3], [4, 5, 6])
plt.title('Size 5x3 inches, DPI 100')
plt.show()

# Create a figure 5x3 inches with 200 dpi
plt.figure(figsize=(5, 3), dpi=200)
plt.plot([1, 2, 3], [4, 5, 6])
plt.title('Size 5x3 inches, DPI 200')
plt.show()
OutputSuccess
Important Notes

Higher DPI means better quality but larger file size.

Figure size is in inches, so bigger numbers mean bigger plots.

If you save a figure, DPI affects the saved image resolution.

Summary

Figure size controls the physical size of the plot in inches.

DPI controls how sharp or detailed the plot image is.

Use both to make your plots look good on screen and paper.