Figure creation with plt.figure in Matplotlib - Time & Space Complexity
When we create a figure using plt.figure(), it takes some time to set up the drawing space.
We want to know how this setup time changes as we create more figures.
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
for i in range(n):
fig = plt.figure()
plt.close(fig)
This code creates and closes n figures one after another.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a figure with
plt.figure()inside a loop. - How many times: Exactly
ntimes, once per loop iteration.
Each figure creation takes roughly the same time, so total time grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 figure creations |
| 100 | 100 figure creations |
| 1000 | 1000 figure creations |
Pattern observation: Doubling n doubles the total work.
Time Complexity: O(n)
This means the time to create n figures grows in a straight line as n increases.
[X] Wrong: "Creating multiple figures at once is instant and does not add up."
[OK] Correct: Each figure requires setup, so time adds up linearly with the number of figures.
Understanding how repeated figure creation scales helps you reason about performance in data visualization tasks.
What if we reused the same figure instead of creating a new one each time? How would the time complexity change?
Practice
plt.figure() function do in matplotlib?Solution
Step 1: Understand the role of plt.figure()
Theplt.figure()function creates a new empty figure, which is like a blank canvas for drawing plots.Step 2: Differentiate from other functions
Functions likeplt.plot()draw on the figure,plt.show()displays it, andplt.savefig()saves it. Onlyplt.figure()creates the blank space.Final Answer:
Creates a new blank canvas for plotting graphs -> Option BQuick Check:
plt.figure() = new blank canvas [OK]
- Confusing plt.figure() with plt.plot()
- Thinking plt.figure() displays the plot
- Assuming plt.figure() saves the plot
plt.figure()?Solution
Step 1: Recall the correct parameter name
The parameter to set figure size isfigsize, which takes a tuple of (width, height) in inches.Step 2: Check the syntax of options
Only plt.figure(figsize=(8, 6)) usesfigsize=(8, 6)correctly. Others use wrong parameter names or data types.Final Answer:
plt.figure(figsize=(8, 6)) -> Option DQuick Check:
figsize=(width, height) = plt.figure(figsize=(8, 6)) [OK]
- Using 'size' instead of 'figsize'
- Passing list instead of tuple
- Using wrong parameter names like 'dimensions'
import matplotlib.pyplot as plt fig = plt.figure(figsize=(4, 3), dpi=100) print(fig.get_size_inches()) print(fig.dpi)
Solution
Step 1: Understand figsize and dpi
Thefigsizeis in inches as a float array, sofig.get_size_inches()returns [4. 3.]. The dpi is dots per inch, set to 100.0.Step 2: Check print outputs
Printingfig.get_size_inches()shows floats with decimal points. Printingfig.dpishows 100.0 as set.Final Answer:
[4. 3.] 100.0 -> Option CQuick Check:
figsize floats and dpi number = [4. 3.] and 100.0 [OK]
- Expecting integers instead of floats for size
- Confusing dpi value with size
- Misreading output format
import matplotlib.pyplot as plt fig = plt.figure(size=(5, 4)) plt.plot([1, 2, 3], [4, 5, 6]) plt.show()
Solution
Step 1: Check parameter names in plt.figure()
The correct parameter to set figure size isfigsize, notsize. Usingsizecauses an error.Step 2: Verify other parts of the code
Usingplt.plot()afterplt.figure()is valid.plt.show()has parentheses. Lists have same length.Final Answer:
The parameter 'size' is invalid; should be 'figsize' -> Option AQuick Check:
Use figsize= not size= in plt.figure() [OK]
- Using 'size' instead of 'figsize'
- Thinking plt.plot() can't follow plt.figure()
- Forgetting parentheses in plt.show()
Solution
Step 1: Understand figure creation for multiple plots
Creating two separate figures requires callingplt.figure()twice, each time creating a new blank canvas.Step 2: Check code correctness
fig1 = plt.figure(figsize=(4,3)) plt.plot([1,2,3],[4,5,6]) fig2 = plt.figure(figsize=(4,3)) plt.plot([1,2,3],[6,5,4]) plt.show() creates two figures with specified sizes and plots on each before showing. plt.figure(figsize=(8,3)) plt.subplot(1,2,1) plt.plot([1,2,3],[4,5,6]) plt.subplot(1,2,2) plt.plot([1,2,3],[6,5,4]) plt.show() uses subplots in one figure, not two figures. fig = plt.figure(figsize=(8,3)) plt.plot([1,2,3],[4,5,6]) plt.plot([1,2,3],[6,5,4]) plt.show() plots both lines on one figure. plt.figure(figsize=(4,3)) plt.plot([1,2,3],[4,5,6]) plt.figure(figsize=(4,3)) plt.plot([1,2,3],[6,5,4]) missesplt.show().Final Answer:
fig1 = plt.figure(figsize=(4,3)) plt.plot([1,2,3],[4,5,6]) fig2 = plt.figure(figsize=(4,3)) plt.plot([1,2,3],[6,5,4]) plt.show() -> Option AQuick Check:
Two plt.figure() calls = two separate plots [OK]
- Using subplots instead of separate figures
- Plotting multiple lines on one figure expecting separate windows
- Forgetting plt.show() to display plots
Ready to prove it?
Pass the test and this pattern moves unknown
