Bird
Raised Fist0
Matplotlibdata~30 mins

Plotting multiple lines in Matplotlib - Mini Project: Build & Apply

Choose your learning style10 modes available
Plotting multiple lines
📖 Scenario: You work as a data analyst for a small company that tracks sales of three products over four months. You want to visualize how sales changed over time for each product.
🎯 Goal: Create a line plot with matplotlib that shows sales trends for three products over four months. Each product's sales should be a separate line on the same graph.
📋 What You'll Learn
Use matplotlib.pyplot to create the plot
Plot three lines, one for each product's sales
Label the x-axis with the months
Add a legend to identify each product's line
💡 Why This Matters
🌍 Real World
Visualizing sales trends helps businesses understand which products are performing well over time and make better decisions.
💼 Career
Data analysts and data scientists often create line plots to compare multiple data series and communicate insights clearly.
Progress0 / 4 steps
1
Create sales data for three products
Create a dictionary called sales_data with keys 'Product A', 'Product B', and 'Product C'. Each key should have a list of four sales numbers: [10, 15, 20, 25] for Product A, [12, 18, 22, 28] for Product B, and [5, 7, 12, 15] for Product C.
Matplotlib
Hint

Use curly braces {} to create a dictionary. Each product name is a key with a list of four numbers as the value.

2
Create a list of months for the x-axis
Create a list called months with the values ['Jan', 'Feb', 'Mar', 'Apr'] to represent the four months.
Matplotlib
Hint

Use square brackets [] to create a list of strings for the months.

3
Plot the sales lines for each product
Import matplotlib.pyplot as plt. Use a for loop with variables product and sales to iterate over sales_data.items(). Inside the loop, call plt.plot(months, sales, label=product) to plot each product's sales line with a label.
Matplotlib
Hint

Remember to import matplotlib.pyplot as plt. Use for product, sales in sales_data.items(): to loop through the dictionary.

4
Add labels, legend, and show the plot
Call plt.xlabel('Month') and plt.ylabel('Sales') to label the axes. Then call plt.legend() to show the legend. Finally, call plt.show() to display the plot.
Matplotlib
Hint

Use plt.xlabel() and plt.ylabel() to label axes. Use plt.legend() to add the legend. Use plt.show() to display the plot window.

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