DPI controls how clear and sharp your plot looks when saved or displayed. Higher DPI means better quality.
DPI settings for resolution in Matplotlib
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
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
Matplotlib
plt.savefig('plot_low.png', dpi=50)
Matplotlib
plt.savefig('plot_high.png', dpi=300)
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.')
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.
Practice
1. What does the
dpi parameter control in matplotlib plots?easy
Solution
Step 1: Understand the role of DPI in images
DPI stands for dots per inch and controls how many pixels are used per inch in an image, affecting sharpness.Step 2: Relate DPI to matplotlib plots
In matplotlib, settingdpichanges the resolution of the saved or displayed plot, making it sharper or blurrier.Final Answer:
The resolution or sharpness of the saved or displayed plot -> Option CQuick Check:
DPI controls resolution = D [OK]
Hint: DPI means dots per inch, controlling image sharpness [OK]
Common Mistakes:
- Confusing DPI with plot size
- Thinking DPI changes plot colors
- Assuming DPI changes plot type
2. Which of the following is the correct way to save a matplotlib plot with 300 DPI resolution?
easy
Solution
Step 1: Recall the correct function to save plots
The correct function to save a plot in matplotlib isplt.savefig().Step 2: Check the parameter for resolution
The parameter to set resolution isdpi, so the correct syntax isplt.savefig('filename', dpi=300).Final Answer:
plt.savefig('plot.png', dpi=300) -> Option DQuick Check:
Use plt.savefig with dpi=300 = A [OK]
Hint: Use plt.savefig(filename, dpi=number) to set resolution [OK]
Common Mistakes:
- Using plt.save instead of plt.savefig
- Using 'resolution' instead of 'dpi'
- Passing dpi as a string '300'
3. What will be the size in pixels of a saved plot with
figsize=(4,3) inches and dpi=200?medium
Solution
Step 1: Calculate width in pixels
Width in pixels = width in inches * dpi = 4 * 200 = 800 pixels.Step 2: Calculate height in pixels
Height in pixels = height in inches * dpi = 3 * 200 = 600 pixels.Final Answer:
800 x 600 pixels -> Option BQuick Check:
Pixels = inches * dpi = 800x600 [OK]
Hint: Multiply inches by dpi for pixel size [OK]
Common Mistakes:
- Confusing dpi with inches
- Multiplying dpi by 100 instead of inches
- Using dpi as pixel count directly
4. Identify the error in this code snippet that tries to save a plot with 150 DPI:
import matplotlib.pyplot as plt
plt.plot([1,2,3],[4,5,6])
plt.savefig('myplot.png', dpi='150')medium
Solution
Step 1: Check the dpi parameter type
The dpi parameter expects an integer number, but here it is passed as a string '150'.Step 2: Understand impact of wrong type
Passing dpi as a string may cause a type error or unexpected behavior when saving the plot.Final Answer:
dpi value should be an integer, not a string -> Option AQuick Check:
dpi must be int, not string = A [OK]
Hint: dpi must be a number, not quoted text [OK]
Common Mistakes:
- Passing dpi as string instead of int
- Thinking plt.show() is needed before savefig
- Assuming file extension affects dpi
5. You want to save a plot with a fixed pixel size of 1200x900 pixels. Which combination of
figsize and dpi will achieve this?hard
Solution
Step 1: Understand pixel size formula
Pixels = figsize (inches) * dpi. We want 1200 x 900 pixels.Step 2: Check each option's pixel size
A: 12*100=1200, 10*100=1000 (wrong)
B: 6*200=1200, 4.5*200=900 (correct)
C: 10*120=1200, 8*120=960 (wrong)
D: 8*150=1200, 7*150=1050 (wrong).Final Answer:
figsize=(6,4.5) and dpi=200 -> Option AQuick Check:
Pixels = inches * dpi = 1200x900 [OK]
Hint: Pixels = figsize * dpi; pick balanced values [OK]
Common Mistakes:
- Choosing too large figsize with low dpi
- Ignoring pixel size formula
- Assuming dpi alone sets pixel size
