0
0
Matplotlibdata~20 mins

Pyplot interface overview in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pyplot Mastery Badge
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 Pyplot code?

Look at this code that uses matplotlib.pyplot. What will it show?

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.title('Simple Line')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
AA line graph with points (1,4), (2,5), (3,6) and labeled axes
BA scatter plot with points (1,4), (2,5), (3,6) but no lines
CA bar chart with bars at 1, 2, 3 with heights 4, 5, 6
DAn empty plot with only axis labels
Attempts:
2 left
💡 Hint

Remember, plt.plot() draws lines connecting points.

data_output
intermediate
1:30remaining
How many lines are drawn by this Pyplot code?

Count the number of lines that will appear after running this code.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.plot([1, 2, 3], [6, 5, 4])
plt.show()
A2
B3
C1
D0
Attempts:
2 left
💡 Hint

Each plt.plot() call adds one line.

visualization
advanced
2:30remaining
What does this Pyplot code visualize?

Examine the code below. What kind of plot will it produce?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
plt.plot(x, y)
plt.fill_between(x, y, color='skyblue', alpha=0.5)
plt.title('Sine Wave with Fill')
plt.show()
AA scatter plot of sine values with blue dots
BA sine wave line plot with the area under the curve shaded in blue
CA bar chart showing sine values at discrete points
DA line plot of cosine wave with shaded area
Attempts:
2 left
💡 Hint

fill_between colors the area under the curve.

🔧 Debug
advanced
1:30remaining
What error does this Pyplot code raise?

Look at this code snippet. What error will it cause when run?

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5])
plt.show()
ASyntaxError: invalid syntax
BTypeError: unsupported operand type(s)
CValueError: x and y must have same first dimension
DNo error, plot will display
Attempts:
2 left
💡 Hint

Check if the x and y lists have the same length.

🚀 Application
expert
3:00remaining
Which option produces a plot with two subplots side by side?

You want to create two plots side by side using Pyplot. Which code will do this correctly?

A
import matplotlib.pyplot as plt
plt.subplots(2, 1)
plt.plot([1, 2, 3], [4, 5, 6])
plt.plot([1, 2, 3], [6, 5, 4])
plt.show()
B
import matplotlib.pyplot as plt
plt.subplot(2, 1)
plt.plot([1, 2, 3], [4, 5, 6])
plt.subplot(2, 2)
plt.plot([1, 2, 3], [6, 5, 4])
plt.show()
C
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2)
axs[0].plot([1, 2, 3], [4, 5, 6])
axs[1].plot([1, 2, 3], [6, 5, 4])
plt.show()
D
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 2)
axs[0].plot([1, 2, 3], [4, 5, 6])
axs[1].plot([1, 2, 3], [6, 5, 4])
plt.show()
Attempts:
2 left
💡 Hint

Use plt.subplots(rows, columns) to create a grid of plots.