Bird
Raised Fist0
Matplotlibdata~5 mins

Plotting multiple lines in Matplotlib

Choose your learning style10 modes available
Introduction

Plotting multiple lines helps you compare different sets of data on the same graph. It makes it easy to see trends and differences side by side.

Comparing sales of different products over months.
Showing temperature changes in multiple cities over time.
Tracking performance of different students in exams.
Visualizing stock prices of several companies on the same chart.
Syntax
Matplotlib
import matplotlib.pyplot as plt

plt.plot(x1, y1, label='Line 1')
plt.plot(x2, y2, label='Line 2')
...
plt.legend()
plt.show()

Use label to name each line for the legend.

Call plt.legend() to show the labels on the plot.

Examples
Plot two lines with the same x values but different y values.
Matplotlib
import matplotlib.pyplot as plt

x = [1, 2, 3]
y1 = [2, 4, 6]
y2 = [1, 3, 5]

plt.plot(x, y1, label='First line')
plt.plot(x, y2, label='Second line')
plt.legend()
plt.show()
Use colors and line styles to make lines different and clear.
Matplotlib
import matplotlib.pyplot as plt

x1 = [0, 1, 2]
y1 = [1, 2, 3]
x2 = [0, 1, 2]
y2 = [3, 2, 1]

plt.plot(x1, y1, 'r-', label='Red line')
plt.plot(x2, y2, 'b--', label='Blue dashed line')
plt.legend()
plt.show()
Sample Program

This program plots sales data for three products over five months. Each product's sales are shown as a separate line with a label. The legend helps identify each line.

Matplotlib
import matplotlib.pyplot as plt

# Data for three lines
months = [1, 2, 3, 4, 5]
sales_A = [10, 15, 20, 25, 30]
sales_B = [12, 18, 22, 28, 35]
sales_C = [8, 14, 19, 23, 29]

# Plot each sales line
plt.plot(months, sales_A, label='Product A')
plt.plot(months, sales_B, label='Product B')
plt.plot(months, sales_C, label='Product C')

# Add labels and title
plt.xlabel('Month')
plt.ylabel('Sales')
plt.title('Monthly Sales Comparison')

# Show legend
plt.legend()

# Display the plot
plt.show()
OutputSuccess
Important Notes

Make sure x and y lists have the same length for each line.

You can customize line colors, styles, and markers for clarity.

Always add a legend when plotting multiple lines to avoid confusion.

Summary

Plot multiple lines by calling plt.plot() multiple times.

Use label and plt.legend() to identify lines.

Customize lines with colors and styles to make the graph clear.

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