Bird
Raised Fist0
Matplotlibdata~10 mins

Memory management with large figures in Matplotlib - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the matplotlib pyplot module correctly.

Matplotlib
import matplotlib.[1] as plt
Drag options to blanks, or click blank then click option'
Acharts
Bpyplot
Cplot
Dfigure
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'plot' instead of 'pyplot'.
Using 'charts' which is not a matplotlib module.
Using 'figure' which is a function, not a module.
2fill in blank
medium

Complete the code to create a new figure with a large size to manage memory better.

Matplotlib
fig = plt.figure(figsize=[1])
Drag options to blanks, or click blank then click option'
A(10, 5)
B(5, 3)
C(20, 15)
D(12, 8)
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing too small figure size like (5, 3).
Choosing too large figure size like (20, 15) which uses more memory.
3fill in blank
hard

Fix the error in the code to properly close a figure and free memory.

Matplotlib
plt.[1](fig)
Drag options to blanks, or click blank then click option'
Aclose
Bclear
Cremove
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'delete' which is not a matplotlib function.
Using 'remove' which does not close figures.
Using 'clear' which clears axes but does not close figures.
4fill in blank
hard

Fill both blanks to create a plot and then clear the current figure to save memory.

Matplotlib
plt.plot(x, y)
plt.[1]()
plt.[2]()
Drag options to blanks, or click blank then click option'
Ashow
Bclose
Cclf
Ddraw
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'close' instead of 'clf' to clear the figure.
Using 'draw' which redraws but does not clear or close.
5fill in blank
hard

Fill all three blanks to create a large figure, plot data, and then close the figure to manage memory.

Matplotlib
fig = plt.figure(figsize=[1])
plt.plot(x, y)
plt.[2]()
plt.[3](fig)
Drag options to blanks, or click blank then click option'
A(15, 10)
Bclose
Cshow
D(8, 6)
Attempts:
3 left
💡 Hint
Common Mistakes
Using too large figure size causing high memory use.
Not closing the figure after plotting.
Calling show after close instead of before.

Practice

(1/5)
1. Why is it important to use plt.close() after creating large figures in matplotlib?
easy
A. To change the color of the figure
B. To free up memory and prevent slowing down the computer
C. To save the figure automatically
D. To increase the size of the figure

Solution

  1. Step 1: Understand memory use by large figures

    Large figures use a lot of computer memory which can slow down the system if not managed.
  2. Step 2: Role of plt.close()

    Using plt.close() frees the memory used by the figure after it is shown or saved.
  3. Final Answer:

    To free up memory and prevent slowing down the computer -> Option B
  4. Quick Check:

    Memory management = Free memory [OK]
Hint: Always close large figures to save memory after use [OK]
Common Mistakes:
  • Thinking plt.close() saves the figure
  • Believing it changes figure appearance
  • Ignoring memory impact of many open figures
2. Which of the following is the correct syntax to close a figure in matplotlib?
easy
A. plt.closeFigure()
B. plt.close_figure()
C. plt.closeFig()
D. plt.close()

Solution

  1. Step 1: Recall matplotlib function names

    The official function to close a figure is plt.close().
  2. Step 2: Check other options

    Other options like plt.close_figure() or plt.closeFig() do not exist in matplotlib.
  3. Final Answer:

    plt.close() -> Option D
  4. Quick Check:

    Correct function name = plt.close() [OK]
Hint: Use exact function names from matplotlib docs [OK]
Common Mistakes:
  • Adding extra words to function name
  • Using camelCase instead of snake_case
  • Confusing with save or show functions
3. What will be the output of the following code snippet?
import matplotlib.pyplot as plt
for i in range(3):
    fig = plt.figure()
    plt.plot([1, 2, 3], [i, i+1, i+2])
plt.show()
medium
A. Three plots will be shown and memory will be freed automatically
B. Only one plot will be shown, others are overwritten
C. Three plots will be shown but memory is not freed, causing high usage
D. Code will raise an error because plt.close() is missing

Solution

  1. Step 1: Analyze the loop creating figures

    The loop creates 3 separate figures and plots on each without closing them.
  2. Step 2: Understand memory impact

    Since plt.close() is not called, all figures stay in memory, increasing usage.
  3. Final Answer:

    Three plots will be shown but memory is not freed, causing high usage -> Option C
  4. Quick Check:

    Figures open without close = high memory [OK]
Hint: Without plt.close(), memory stays used after plotting [OK]
Common Mistakes:
  • Assuming memory frees automatically after plt.show()
  • Thinking only one plot appears
  • Expecting an error without plt.close()
4. Identify the error in this code that creates multiple large figures:
import matplotlib.pyplot as plt
for i in range(5):
    fig = plt.figure(figsize=(10,8))
    plt.plot([1,2,3], [i,i+1,i+2])
    plt.show()
medium
A. Missing plt.close() to free memory after each figure
B. plt.show() should be outside the loop
C. Figure size is too small for large data
D. Plot data lists have different lengths

Solution

  1. Step 1: Check memory management in loop

    The code creates large figures repeatedly but never closes them, causing memory buildup.
  2. Step 2: Identify missing memory freeing step

    Adding plt.close() after plt.show() frees memory for each figure.
  3. Final Answer:

    Missing plt.close() to free memory after each figure -> Option A
  4. Quick Check:

    Close figures in loops to avoid memory leaks [OK]
Hint: Always close figures inside loops after showing [OK]
Common Mistakes:
  • Moving plt.show() outside loop without closing figures
  • Changing figure size instead of closing
  • Ignoring memory issues with many figures
5. You need to generate 100 large plots in a script without running out of memory. Which approach is best to manage memory efficiently?
hard
A. Create each figure, plot data, save it, then call plt.close() before next
B. Create all 100 figures first, then plot and save them all together
C. Plot all data on one figure without closing it
D. Use plt.show() after all figures are created without closing

Solution

  1. Step 1: Understand memory use when creating many figures

    Creating many large figures without closing them uses too much memory and slows the system.
  2. Step 2: Best practice for memory management

    Creating, saving, then closing each figure before the next frees memory and avoids overload.
  3. Final Answer:

    Create each figure, plot data, save it, then call plt.close() before next -> Option A
  4. Quick Check:

    Close each figure after saving to save memory [OK]
Hint: Save and close each figure before next to avoid memory issues [OK]
Common Mistakes:
  • Creating all figures before saving causes memory overload
  • Not closing figures after plotting
  • Plotting all data on one figure when separate plots needed