Bird
Raised Fist0
Matplotlibdata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the plt.figure() function do in matplotlib?
easy
A. Plots a line graph directly
B. Creates a new blank canvas for plotting graphs
C. Saves the current plot to a file
D. Displays the plot on the screen

Solution

  1. Step 1: Understand the role of plt.figure()

    The plt.figure() function creates a new empty figure, which is like a blank canvas for drawing plots.
  2. Step 2: Differentiate from other functions

    Functions like plt.plot() draw on the figure, plt.show() displays it, and plt.savefig() saves it. Only plt.figure() creates the blank space.
  3. Final Answer:

    Creates a new blank canvas for plotting graphs -> Option B
  4. Quick Check:

    plt.figure() = new blank canvas [OK]
Hint: Remember: figure = blank canvas for plots [OK]
Common Mistakes:
  • Confusing plt.figure() with plt.plot()
  • Thinking plt.figure() displays the plot
  • Assuming plt.figure() saves the plot
2. Which of the following is the correct way to create a figure with size 8 inches wide and 6 inches tall using plt.figure()?
easy
A. plt.figure(size=(8, 6))
B. plt.figure(dimensions=(8, 6))
C. plt.figure(size=[8, 6])
D. plt.figure(figsize=(8, 6))

Solution

  1. Step 1: Recall the correct parameter name

    The parameter to set figure size is figsize, which takes a tuple of (width, height) in inches.
  2. Step 2: Check the syntax of options

    Only plt.figure(figsize=(8, 6)) uses figsize=(8, 6) correctly. Others use wrong parameter names or data types.
  3. Final Answer:

    plt.figure(figsize=(8, 6)) -> Option D
  4. Quick Check:

    figsize=(width, height) = plt.figure(figsize=(8, 6)) [OK]
Hint: Use figsize=(width, height) tuple inside plt.figure() [OK]
Common Mistakes:
  • Using 'size' instead of 'figsize'
  • Passing list instead of tuple
  • Using wrong parameter names like 'dimensions'
3. What will be the output of this code?
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(4, 3), dpi=100)
print(fig.get_size_inches())
print(fig.dpi)
medium
A. [4 3] 100.0
B. [400 300] 100.0
C. [4. 3.] 100.0
D. [4.0 3.0] 10

Solution

  1. Step 1: Understand figsize and dpi

    The figsize is in inches as a float array, so fig.get_size_inches() returns [4. 3.]. The dpi is dots per inch, set to 100.0.
  2. Step 2: Check print outputs

    Printing fig.get_size_inches() shows floats with decimal points. Printing fig.dpi shows 100.0 as set.
  3. Final Answer:

    [4. 3.] 100.0 -> Option C
  4. Quick Check:

    figsize floats and dpi number = [4. 3.] and 100.0 [OK]
Hint: fig.get_size_inches() returns floats, dpi is float [OK]
Common Mistakes:
  • Expecting integers instead of floats for size
  • Confusing dpi value with size
  • Misreading output format
4. Identify the error in this code snippet:
import matplotlib.pyplot as plt
fig = plt.figure(size=(5, 4))
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
medium
A. The parameter 'size' is invalid; should be 'figsize'
B. plt.plot() cannot be used after plt.figure()
C. plt.show() is missing parentheses
D. The lists in plt.plot() have different lengths

Solution

  1. Step 1: Check parameter names in plt.figure()

    The correct parameter to set figure size is figsize, not size. Using size causes an error.
  2. Step 2: Verify other parts of the code

    Using plt.plot() after plt.figure() is valid. plt.show() has parentheses. Lists have same length.
  3. Final Answer:

    The parameter 'size' is invalid; should be 'figsize' -> Option A
  4. Quick Check:

    Use figsize= not size= in plt.figure() [OK]
Hint: Use figsize=, not size=, to set figure size [OK]
Common Mistakes:
  • Using 'size' instead of 'figsize'
  • Thinking plt.plot() can't follow plt.figure()
  • Forgetting parentheses in plt.show()
5. You want to create two separate plots in two separate windows. Which code correctly creates two figures and plots a simple line in each?
hard
A. 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()
B. 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()
C. fig = plt.figure(figsize=(8,3)) plt.plot([1,2,3],[4,5,6]) plt.plot([1,2,3],[6,5,4]) plt.show()
D. 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])

Solution

  1. Step 1: Understand figure creation for multiple plots

    Creating two separate figures requires calling plt.figure() twice, each time creating a new blank canvas.
  2. 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]) misses plt.show().
  3. 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 A
  4. Quick Check:

    Two plt.figure() calls = two separate plots [OK]
Hint: Call plt.figure() twice for separate plot windows [OK]
Common Mistakes:
  • Using subplots instead of separate figures
  • Plotting multiple lines on one figure expecting separate windows
  • Forgetting plt.show() to display plots
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard