Challenge - 5 Problems
Master of Plotting Multiple Lines
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of plotting multiple lines with labels
What will be the output of this code snippet that plots two lines with labels and a legend?
Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3] y1 = [2, 3, 5] y2 = [3, 5, 7] plt.plot(x, y1, label='Line 1') plt.plot(x, y2, label='Line 2') plt.legend() plt.show()
Attempts:
2 left
💡 Hint
Look at how labels and plt.legend() work together to show the legend.
✗ Incorrect
The code plots two lines with labels and calls plt.legend(), so the legend shows both labels.
❓ data_output
intermediate1:30remaining
Number of lines plotted
How many lines will be drawn on the plot after running this code?
Matplotlib
import matplotlib.pyplot as plt x = [0, 1, 2, 3] for i in range(3): y = [j * (i + 1) for j in x] plt.plot(x, y) plt.show()
Attempts:
2 left
💡 Hint
Count how many times plt.plot is called inside the loop.
✗ Incorrect
The loop runs 3 times, each time plotting one line, so 3 lines total.
🔧 Debug
advanced2:00remaining
Identify the error in plotting multiple lines
What error will this code produce when trying to plot multiple lines?
Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3] y1 = [2, 3] y2 = [3, 5, 7] plt.plot(x, y1) plt.plot(x, y2) plt.show()
Attempts:
2 left
💡 Hint
Check if x and y lists have the same length for each plot call.
✗ Incorrect
y1 has length 2 but x has length 3, causing a ValueError about dimension mismatch.
❓ visualization
advanced2:00remaining
Effect of linestyle on multiple lines
Which option describes the visual difference when plotting these two lines with different linestyles?
Matplotlib
import matplotlib.pyplot as plt x = [0, 1, 2] y1 = [1, 2, 3] y2 = [3, 2, 1] plt.plot(x, y1, linestyle='--') plt.plot(x, y2, linestyle=':') plt.show()
Attempts:
2 left
💡 Hint
Look up what '--' and ':' mean for linestyles in matplotlib.
✗ Incorrect
'--' means dashed line, ':' means dotted line, so lines appear differently.
🚀 Application
expert3:00remaining
Plot multiple lines with different colors and labels
You want to plot three lines with x values [1,2,3], y values as multiples of x (1x, 2x, 3x), each with a different color and label. Which code produces the correct plot with legend?
Attempts:
2 left
💡 Hint
Check how labels and colors are assigned and how enumerate is used.
✗ Incorrect
Option D correctly assigns colors and labels using enumerate starting at 1, so labels match line multiples and legend shows all lines.