0
0
Matplotlibdata~20 mins

Figure creation with plt.figure in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Figure Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the size of the figure created?
Consider the following code snippet using matplotlib:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8, 4))
print(fig.get_size_inches())

What will be the output of this code?
Matplotlib
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8, 4))
print(fig.get_size_inches())
A[8. 4.]
B[4. 8.]
C[0 0]
DError: AttributeError
Attempts:
2 left
💡 Hint
The figsize parameter sets the width and height in inches.
data_output
intermediate
2:00remaining
How many axes are in the figure?
Given this code:
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
print(len(fig.axes))

What is the printed output?
Matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
print(len(fig.axes))
A1
BError: AttributeError
C0
D2
Attempts:
2 left
💡 Hint
Each add_subplot call adds one axes to the figure.
visualization
advanced
2:30remaining
What does the figure look like?
Examine this code:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(6, 3))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.plot([1, 2, 3], [4, 5, 6])
plt.show()

Which description best matches the figure displayed?
Matplotlib
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(6, 3))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.plot([1, 2, 3], [4, 5, 6])
plt.show()
AA tall square figure with two side-by-side plots.
BA figure with multiple overlapping plots.
CA wide rectangular figure with a single plot filling most of the area.
DA small figure with no plots visible.
Attempts:
2 left
💡 Hint
figsize sets width and height; add_axes controls plot position and size.
🔧 Debug
advanced
2:00remaining
Why does this code raise an error?
Look at this code:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=8, 4)
ax = fig.add_subplot(111)
plt.show()

Why does it raise an error?
Matplotlib
import matplotlib.pyplot as plt
fig = plt.figure(figsize=8, 4)
ax = fig.add_subplot(111)
plt.show()
ABecause figsize must be a tuple, not two separate arguments.
BBecause add_subplot(111) is invalid syntax.
CBecause plt.show() must be called before creating the figure.
DBecause matplotlib is not imported correctly.
Attempts:
2 left
💡 Hint
Check the syntax of figsize parameter.
🚀 Application
expert
3:00remaining
How to create two figures with different sizes?
You want to create two separate figures with sizes 5x5 and 10x2 inches respectively. Which code correctly creates these figures and prints their sizes?
Matplotlib
import matplotlib.pyplot as plt
fig1 = plt.figure(figsize=(5, 5))
fig2 = plt.figure(figsize=(10, 2))
print(fig1.get_size_inches())
print(fig2.get_size_inches())
A
fig1 = plt.figure(figsize=5, 5)
fig2 = plt.figure(figsize=10, 2)
print(fig1.get_size_inches())
print(fig2.get_size_inches())
B
fig1 = plt.figure(figsize=(5, 5))
fig2 = plt.figure(figsize=(10, 2))
print(fig1.get_size_inches())
print(fig2.get_size_inches())
C
fig1 = plt.figure(figsize=[5, 5])
fig2 = plt.figure(figsize=[10, 2])
print(fig1.get_size_inches())
print(fig2.get_size_inches())
D
fig1 = plt.figure(figsize=(5))
fig2 = plt.figure(figsize=(10))
print(fig1.get_size_inches())
print(fig2.get_size_inches())
Attempts:
2 left
💡 Hint
figsize must be a tuple of two numbers.