0
0
Matplotlibdata~20 mins

How Matplotlib renders figures - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Matplotlib Rendering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Matplotlib code?

Consider the following Python code using Matplotlib. What will be the output when this code runs?

Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
print(type(fig))
A<class 'matplotlib.figure.Figure'>
B<class 'matplotlib.axes.Axes'>
C<class 'list'>
D<class 'tuple'>
Attempts:
2 left
💡 Hint

Remember, plt.subplots() returns a figure and axes object. The variable fig stores the figure.

data_output
intermediate
2:00remaining
How many axes are created by this code?

Look at this code snippet. How many axes objects are created?

Matplotlib
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2)
print(len(axs.flatten()))
A4
B0
C1
D2
Attempts:
2 left
💡 Hint

Think about the grid size in plt.subplots(2, 2).

visualization
advanced
2:30remaining
Which option shows the correct figure rendering order?

Matplotlib renders figure elements in a specific order. Which option correctly orders these steps?

  • 1. Drawing axes
  • 2. Creating figure canvas
  • 3. Rendering figure to screen
  • 4. Drawing figure background
A1, 2, 4, 3
B2, 4, 1, 3
C2, 1, 4, 3
D4, 2, 1, 3
Attempts:
2 left
💡 Hint

Think about what happens first: setting up the canvas, then background, then axes, then showing.

🔧 Debug
advanced
2:00remaining
What error does this code raise?

What error will this Matplotlib code produce?

Matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1, 2, 3], [4, 5])
plt.show()
AAttributeError: 'AxesSubplot' object has no attribute 'plot'
BTypeError: 'int' object is not iterable
CValueError: x and y must have same first dimension
DNo error, plot displays correctly
Attempts:
2 left
💡 Hint

Check the lengths of the x and y data lists passed to plot().

🚀 Application
expert
2:30remaining
What is the final size of the saved figure in pixels?

This code saves a figure. What is the size in pixels of the saved image?

Matplotlib
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(4, 3), dpi=150)
fig.savefig('test.png')
A450 x 600 pixels
B4 x 3 pixels
C150 x 150 pixels
D600 x 450 pixels
Attempts:
2 left
💡 Hint

Multiply figure size in inches by dpi to get pixels.