0
0
Matplotlibdata~30 mins

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

Choose your learning style9 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
Need a 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
Need a 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
Need a 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
Need a 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.