Bird
Raised Fist0
Matplotlibdata~20 mins

Plotting multiple lines in Matplotlib - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main way to plot multiple lines on the same graph using matplotlib?
easy
A. Use plt.hist() multiple times
B. Use plt.scatter() for each line
C. Call plt.plot() multiple times with different data sets before plt.show()
D. Call plt.bar() for each line

Solution

  1. Step 1: Understand plotting multiple lines

    In matplotlib, to plot multiple lines on the same graph, you call plt.plot() for each line's data before showing the plot.
  2. Step 2: Identify correct plotting function

    plt.scatter(), plt.bar(), and plt.hist() are for scatter plots, bar charts, and histograms, not line plots.
  3. Final Answer:

    Call plt.plot() multiple times with different data sets before plt.show() -> Option C
  4. Quick Check:

    Multiple plt.plot() calls = multiple lines [OK]
Hint: Use multiple plt.plot() calls to add lines [OK]
Common Mistakes:
  • Using plt.scatter() instead of plt.plot() for lines
  • Calling plt.show() after each line instead of once
  • Using bar or histogram functions for line plots
2. Which of the following code snippets correctly plots two lines on the same graph?
easy
A. plt.plot([1, 2, 3], [4, 5, 6]) plt.plot([1, 2, 3], [6, 5, 4]) plt.show()
B. plt.plot([1, 2, 3], [4, 5, 6]) plt.show() plt.plot([1, 2, 3], [6, 5, 4])
C. plt.show() plt.plot([1, 2, 3], [4, 5, 6]) plt.plot([1, 2, 3], [6, 5, 4])
D. plt.plot([1, 2, 3], [4, 5, 6]) plt.plot([1, 2, 3], [6, 5, 4])

Solution

  1. Step 1: Check order of plotting and showing

    To plot multiple lines, call plt.plot() for each line, then call plt.show() once at the end.
  2. Step 2: Identify correct snippet

    plt.plot([1, 2, 3], [4, 5, 6]) plt.plot([1, 2, 3], [6, 5, 4]) plt.show() calls plt.plot() twice then plt.show(), which is correct. plt.plot([1, 2, 3], [4, 5, 6]) plt.show() plt.plot([1, 2, 3], [6, 5, 4]) calls plt.show() too early, splitting the plots. plt.show() plt.plot([1, 2, 3], [4, 5, 6]) plt.plot([1, 2, 3], [6, 5, 4]) calls plt.show() before plotting, so nothing shows. plt.plot([1, 2, 3], [4, 5, 6]) plt.plot([1, 2, 3], [6, 5, 4]) misses plt.show() entirely.
  3. Final Answer:

    Two plt.plot() calls followed by plt.show() -> Option A
  4. Quick Check:

    Plot lines first, show once after [OK]
Hint: Call plt.show() only after all plt.plot() calls [OK]
Common Mistakes:
  • Calling plt.show() before all plots
  • Forgetting plt.show() to display plot
  • Plotting after plt.show() call
3. What will be the output of this code?
import matplotlib.pyplot as plt
x = [1, 2, 3]
y1 = [2, 4, 6]
y2 = [1, 3, 5]
plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.legend()
plt.show()
medium
A. A graph with two lines but no legend
B. A graph with two lines labeled 'Line 1' and 'Line 2'
C. A graph with one line labeled 'Line 2' only
D. No graph displayed due to error

Solution

  1. Step 1: Analyze plotting commands

    The code plots two lines with plt.plot() and assigns labels 'Line 1' and 'Line 2'.
  2. Step 2: Understand legend usage

    Calling plt.legend() displays the legend with the labels for both lines.
  3. Final Answer:

    A graph with two lines labeled 'Line 1' and 'Line 2' -> Option B
  4. Quick Check:

    Labels + plt.legend() = labeled lines [OK]
Hint: Use plt.legend() to show labels for all lines [OK]
Common Mistakes:
  • Forgetting plt.legend() so labels don't show
  • Assigning labels incorrectly
  • Plotting only one line
4. Identify the error in this code that tries to plot two lines:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y1 = [2, 4, 6]
y2 = [1, 3, 5]
plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.legend
plt.show()
medium
A. Missing parentheses after plt.legend() call
B. x and y lists have different lengths
C. plt.show() is called before plotting
D. Labels are not assigned to lines

Solution

  1. Step 1: Check function calls

    The code calls plt.legend without parentheses, so the legend function is not executed.
  2. Step 2: Understand effect of missing parentheses

    Without calling plt.legend(), the legend does not appear, so labels won't show.
  3. Final Answer:

    Missing parentheses after plt.legend() call -> Option A
  4. Quick Check:

    Call plt.legend() with () to show legend [OK]
Hint: Always add () after plt.legend to display legend [OK]
Common Mistakes:
  • Forgetting () after plt.legend
  • Assuming legend shows without calling function
  • Mixing up order of plt.show()
5. You want to plot three lines with different colors and styles on the same graph and add a legend. Which code snippet correctly achieves this?
hard
A. plt.plot(x, y1, 'r-', label='Red solid') plt.plot(x, y2, 'g--') plt.plot(x, y3, 'b:') plt.legend() plt.show()
B. plt.plot(x, y1, color='red', linestyle='-', label='Red solid') plt.plot(x, y2, color='green', linestyle='--', label='Green dashed') plt.plot(x, y3, color='blue', linestyle=':', label='Blue dotted') plt.legend() plt.show()
C. plt.plot(x, y1, 'red', 'solid', label='Red solid') plt.plot(x, y2, 'green', 'dashed', label='Green dashed') plt.plot(x, y3, 'blue', 'dotted', label='Blue dotted') plt.legend() plt.show()
D. plt.plot(x, y1, 'r-', label='Red solid') plt.plot(x, y2, 'g--', label='Green dashed') plt.plot(x, y3, 'b:', label='Blue dotted') plt.legend() plt.show()

Solution

  1. Step 1: Understand color and style shorthand

    In matplotlib, shorthand like 'r-' means red solid line, 'g--' green dashed, 'b:' blue dotted.
  2. Step 2: Check label usage and legend

    plt.plot(x, y1, 'r-', label='Red solid') plt.plot(x, y2, 'g--', label='Green dashed') plt.plot(x, y3, 'b:', label='Blue dotted') plt.legend() plt.show() assigns labels to all lines and calls plt.legend() to show them. plt.plot(x, y1, color='red', linestyle='solid', label='Red solid') plt.plot(x, y2, color='green', linestyle='dashed', label='Green dashed') plt.plot(x, y3, color='blue', linestyle='dotted', label='Blue dotted') plt.legend() plt.show() uses invalid linestyle names ('solid', 'dashed', 'dotted' are not valid; correct are '-', '--', ':'). plt.plot(x, y1, 'red', 'solid', label='Red solid') plt.plot(x, y2, 'green', 'dashed', label='Green dashed') plt.plot(x, y3, 'blue', 'dotted', label='Blue dotted') plt.legend() plt.show() uses positional strings incorrectly. plt.plot(x, y1, 'r-', label='Red solid') plt.plot(x, y2, 'g--') plt.plot(x, y3, 'b:') plt.legend() plt.show() misses labels for second and third lines.
  3. Final Answer:

    Shorthand format strings like 'r-', 'g--', 'b:' for each plt.plot() -> Option D
  4. Quick Check:

    Use shorthand 'color-style' and label each line [OK]
Hint: Use shorthand like 'r-', 'g--', 'b:' with labels [OK]
Common Mistakes:
  • Using invalid linestyle names like 'solid'
  • Forgetting labels on all lines
  • Not calling plt.legend() to show labels
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