0
0
Matplotlibdata~20 mins

Memory management with large figures in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Memory Mastery with Large Figures
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the memory usage behavior of this matplotlib code?

Consider the following Python code that creates and closes a large figure repeatedly. What will be the output regarding memory usage?

Matplotlib
import matplotlib.pyplot as plt
import tracemalloc

tracemalloc.start()

for i in range(3):
    fig, ax = plt.subplots(figsize=(20, 20))
    ax.plot(range(100000))
    plt.close(fig)

current, peak = tracemalloc.get_traced_memory()
print(f"Current memory usage: {current / 10**6:.2f} MB")
print(f"Peak memory usage: {peak / 10**6:.2f} MB")
tracemalloc.stop()
ACurrent memory usage: less than 10 MB; Peak memory usage: around 50 MB
BCurrent memory usage: more than 100 MB; Peak memory usage: more than 150 MB
CCurrent memory usage: less than 10 MB; Peak memory usage: less than 10 MB
DCurrent memory usage: more than 100 MB; Peak memory usage: less than 10 MB
Attempts:
2 left
💡 Hint

Think about how closing figures affects memory in matplotlib.

🧠 Conceptual
intermediate
1:30remaining
Why is it important to close figures in matplotlib when working with large data?

When plotting large datasets repeatedly, why should you close figures explicitly in matplotlib?

ATo automatically save the figures to disk
BTo free up memory and avoid memory leaks that slow down the program
CTo make the plots display faster on the screen
DTo reduce the size of the saved image files
Attempts:
2 left
💡 Hint

Think about what happens if figures stay open in memory.

🔧 Debug
advanced
2:00remaining
Identify the cause of increasing memory usage in this plotting loop

Look at this code snippet that plots data in a loop. Memory usage increases after each iteration. What is the cause?

Matplotlib
import matplotlib.pyplot as plt

for i in range(5):
    fig, ax = plt.subplots()
    ax.plot(range(10000))
    # Missing plt.close(fig)
    print(f"Iteration {i} done")
AFigures are not closed, so memory accumulates with each iteration
BThe plot data is too large to fit in memory
CThe loop variable i is not used inside the loop
DThe print statement causes memory leaks
Attempts:
2 left
💡 Hint

Think about what happens to figures that are not closed.

data_output
advanced
1:30remaining
What is the number of open figures after this code runs?

After running the following code, how many figures remain open in matplotlib?

Matplotlib
import matplotlib.pyplot as plt

for _ in range(3):
    plt.figure()
plt.close(1)
plt.close(3)
A2
B0
C3
D1
Attempts:
2 left
💡 Hint

Figure numbers start at 1 and increase with each new figure.

🚀 Application
expert
2:30remaining
Optimize memory usage when generating many large plots

You need to generate 100 large plots in a script. Which approach best manages memory to avoid crashes?

ACreate all 100 plots first, then save and close them all at the end
BUse plt.show() after each plot to clear memory automatically
CCreate each plot with plt.subplots(), save it, then call plt.close() on the figure before next iteration
DCreate each plot and save it without closing figures, relying on Python garbage collection
Attempts:
2 left
💡 Hint

Think about when memory is freed during plotting.