Bird
Raised Fist0
Matplotlibdata~10 mins

Plotting multiple lines in Matplotlib - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to plot two lines on the same graph using matplotlib.

Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3]
y1 = [2, 4, 6]
y2 = [1, 3, 5]
plt.plot(x, y1)
plt.[1](x, y2)
plt.show()
Drag options to blanks, or click blank then click option'
Aplot
Bscatter
Cbar
Dhist
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.scatter or plt.bar instead of plt.plot for lines.
Forgetting to call plt.plot for the second line.
2fill in blank
medium

Complete the code to add labels to each line in the plot.

Matplotlib
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, [1]='Line 2')
plt.legend()
plt.show()
Drag options to blanks, or click blank then click option'
Atitle
Blabel
Cxlabel
Dylabel
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'title' or 'xlabel' instead of 'label' for line names.
Forgetting to call plt.legend() to display labels.
3fill in blank
hard

Fix the error in the code to plot multiple lines with different colors.

Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3]
y1 = [2, 4, 6]
y2 = [1, 3, 5]
plt.plot(x, y1, color='red')
plt.plot(x, y2, color='[1]')
plt.show()
Drag options to blanks, or click blank then click option'
Ayellowish
B3
Cblue
Dgreenish
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid color names or numbers for the color argument.
Forgetting quotes around color names.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each x to its y1 squared, but only if y1 is greater than 3.

Matplotlib
result = {x: y1[1]2 for x, y1 in zip(x, y1) if y1 [2] 3}
Drag options to blanks, or click blank then click option'
A**
B>
C<
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' instead of '**' for power.
Using '<' instead of '>' in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each line label to the sum of its y values if the sum is greater than 5.

Matplotlib
data = {'Line 1': y1, 'Line 2': y2}
result = [1]: sum([2]) for [1], [2] in data.items() if sum([2]) > 5}
Drag options to blanks, or click blank then click option'
Alabel
Bvalues
Cy
Dline
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not using the variable names from the options pool.

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