0
0
Matplotlibdata~10 mins

Figure size and DPI in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Figure size and DPI
Create Figure with size (width, height)
Set DPI (dots per inch) for resolution
Plot data on figure canvas
Render figure with specified size and DPI
Display or save the figure image
This flow shows how matplotlib creates a figure with a set size and resolution (DPI), then plots data and renders the image accordingly.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(4, 3), dpi=100)
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
Creates a 4x3 inch figure with 100 DPI, plots a simple line, and displays the figure.
Execution Table
StepActionParameter/ValueEffect on FigureOutput
1Create figurefigsize=(4,3)Figure size set to 4 inches wide, 3 inches tallFigure object created
2Set DPIdpi=100Resolution set to 100 dots per inchFigure resolution defined
3Plot datax=[1,2,3], y=[4,5,6]Line plotted on figure canvasLine plot ready
4Render figuresize=4x3 inches, dpi=100Canvas pixels = 400x300 (4*100 by 3*100)Figure rendered with correct size and resolution
5Display figureplt.show()Figure window opens showing plotVisual output displayed
6ExitEnd of codeNo further actionExecution stops
💡 Code finishes after displaying the figure window
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
figsizeNone(4, 3)(4, 3)(4, 3)(4, 3)(4, 3)
dpiNoneNone100100100100
figure_pixelsNoneNoneNoneNone(400, 300)(400, 300)
plot_dataNoneNoneNonex=[1,2,3], y=[4,5,6]x=[1,2,3], y=[4,5,6]x=[1,2,3], y=[4,5,6]
Key Moments - 2 Insights
Why does changing DPI affect the pixel size but not the figure size in inches?
Because DPI means dots per inch, it controls how many pixels fit in each inch. The figure size in inches stays the same, but higher DPI means more pixels, so higher resolution. See execution_table step 4 where 4 inches * 100 DPI = 400 pixels.
What happens if we set figsize but not DPI?
Matplotlib uses a default DPI (usually 100). So the figure size in inches is set, but pixel size depends on default DPI. This is like step 1 without step 2 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4. What is the pixel size of the figure canvas?
A400x300 pixels
B4x3 pixels
C100x100 pixels
D40x30 pixels
💡 Hint
Check the 'Effect on Figure' column at step 4 in the execution_table.
According to variable_tracker, what is the DPI value after step 2?
ANone
B50
C100
D300
💡 Hint
Look at the 'dpi' row under 'After Step 2' in variable_tracker.
If we change figsize to (8, 6) but keep dpi=100, what will be the pixel size of the figure?
A400x300 pixels
B800x600 pixels
C100x100 pixels
D1600x1200 pixels
💡 Hint
Multiply width and height in inches by DPI as shown in execution_table step 4.
Concept Snapshot
matplotlib.figure(figsize=(width, height), dpi=resolution)
- figsize sets figure size in inches
- dpi sets dots per inch (pixel density)
- Pixel size = figsize * dpi
- Higher dpi means sharper images
- Use plt.show() to display the figure
Full Transcript
This visual execution trace shows how matplotlib creates a figure with a specific size and resolution. First, the figure is created with a size in inches (figsize). Then, the DPI (dots per inch) sets how many pixels fit in each inch, affecting resolution. Data is plotted on the figure canvas. The figure is rendered with pixel dimensions calculated by multiplying figsize by dpi. Finally, the figure is displayed. Variables like figsize, dpi, and pixel size change step by step. Key moments clarify why DPI affects pixels but not inches, and what happens if DPI is not set. The quiz tests understanding of pixel size, DPI value, and effects of changing figsize.