📖 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
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.
Step 2: Identify correct plotting function
plt.scatter(), plt.bar(), and plt.hist() are for scatter plots, bar charts, and histograms, not line plots.
Final Answer:
Call plt.plot() multiple times with different data sets before plt.show() -> Option C
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?