0
0
Matplotlibdata~20 mins

Plotting multiple lines in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Plotting Multiple Lines
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()
AA plot with two lines labeled 'Line 1' and 'Line 2' and a legend showing these labels.
BA plot with two lines but no legend or labels shown.
CA plot with two lines but the legend shows only 'Line 2'.
DA plot with one line only labeled 'Line 1'.
Attempts:
2 left
💡 Hint
Look at how labels and plt.legend() work together to show the legend.
data_output
intermediate
1: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()
A4
B3
C1
D0
Attempts:
2 left
💡 Hint
Count how many times plt.plot is called inside the loop.
🔧 Debug
advanced
2: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()
AValueError: x and y must have same first dimension
BTypeError: 'list' object is not callable
CNo error, plots two lines correctly
DNameError: name 'plt' is not defined
Attempts:
2 left
💡 Hint
Check if x and y lists have the same length for each plot call.
visualization
advanced
2: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()
ABoth lines are solid.
BOne line is dotted and the other is solid.
COne line is dashed and the other is dotted.
DBoth lines are dashed.
Attempts:
2 left
💡 Hint
Look up what '--' and ':' mean for linestyles in matplotlib.
🚀 Application
expert
3: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?
A
import matplotlib.pyplot as plt
x = [1, 2, 3]
for i, color in enumerate(['red', 'green', 'blue']):
    y = [i * val for val in x]
    plt.plot(x, y, color=color)
plt.legend()
plt.show()
B
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [val for val in x]
plt.plot(x, y, color='red', label='Line 1')
plt.plot(x, [2*val for val in x], color='green')
plt.plot(x, [3*val for val in x], color='blue', label='Line 3')
plt.legend()
plt.show()
C
import matplotlib.pyplot as plt
x = [1, 2, 3]
for i in range(1, 4):
    y = [i * val for val in x]
    plt.plot(x, y, label='Line i')
plt.legend()
plt.show()
D
import matplotlib.pyplot as plt
x = [1, 2, 3]
for i, color in enumerate(['red', 'green', 'blue'], 1):
    y = [i * val for val in x]
    plt.plot(x, y, color=color, label=f'Line {i}')
plt.legend()
plt.show()
Attempts:
2 left
💡 Hint
Check how labels and colors are assigned and how enumerate is used.