📖 Scenario: You are a data analyst working with monthly sales data for a small store. You want to visualize how sales changed over the first six months of the year.
🎯 Goal: Create a line plot to show monthly sales trends from January to June.
📋 What You'll Learn
Create a dictionary with months as keys and sales numbers as values
Create a list of months in order
Use matplotlib to plot sales data as a line plot
Label the x-axis as 'Month' and y-axis as 'Sales'
Display the plot
💡 Why This Matters
🌍 Real World
Line plots are used to show trends over time, like sales, temperature, or stock prices.
💼 Career
Data analysts and scientists use line plots to communicate patterns and changes clearly to others.
Progress0 / 4 steps
1
Create the sales data dictionary
Create a dictionary called sales_data with these exact entries: 'Jan': 150, 'Feb': 200, 'Mar': 180, 'Apr': 220, 'May': 210, 'Jun': 230.
Data Analysis Python
Hint
Use curly braces {} to create a dictionary with month names as keys and sales numbers as values.
2
Create the list of months in order
Create a list called months with these exact values in order: 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'.
Data Analysis Python
Hint
Use square brackets [] to create a list of months in the correct order.
3
Plot the sales data as a line plot
Import matplotlib.pyplot as plt. Create a list called sales that contains the sales numbers from sales_data in the order of months. Use plt.plot(months, sales) to create a line plot.
Data Analysis Python
Hint
Use a list comprehension to get sales numbers in order. Then use plt.plot() to draw the line plot.
4
Add labels and display the plot
Use plt.xlabel('Month') and plt.ylabel('Sales') to label the axes. Then use plt.show() to display the plot.
Data Analysis Python
Hint
Use plt.xlabel() and plt.ylabel() to add axis labels. Then call plt.show() to display the plot window.
Practice
(1/5)
1. What is the main purpose of a line plot in data analysis?
easy
A. To display data as separate bars for comparison
B. To list raw data values without visualization
C. To show the distribution of data using boxes
D. To show trends or changes over time by connecting data points
Solution
Step 1: Understand the function of line plots
Line plots connect points to show how values change over time or order.
Step 2: Compare with other plot types
Bar plots show separate bars, box plots show distribution, and raw data listing is not a plot.
Final Answer:
To show trends or changes over time by connecting data points -> Option D
Quick Check:
Line plot = trends over time [OK]
Hint: Line plots connect points to show trends clearly [OK]
Common Mistakes:
Confusing line plots with bar charts
Thinking line plots show data distribution
Assuming line plots display raw data only
2. Which of the following is the correct way to create a simple line plot using matplotlib in Python?
easy
A. plt.graph(x, y)
B. plt.line(x, y)
C. plt.plot(x, y)
D. plt.chart(x, y)
Solution
Step 1: Recall matplotlib line plot syntax
The function to create line plots in matplotlib is plt.plot().
Step 2: Check other options
Functions like plt.line(), plt.graph(), and plt.chart() do not exist in matplotlib.
Final Answer:
plt.plot(x, y) -> Option C
Quick Check:
matplotlib line plot = plt.plot() [OK]
Hint: Use plt.plot() to draw line plots in matplotlib [OK]
Common Mistakes:
Using non-existent functions like plt.line()
Confusing plot with chart or graph functions
Forgetting to import matplotlib.pyplot as plt
3. What will be the output of the following code?
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 15, 25]
plt.plot(x, y, marker='o')
plt.show()
medium
A. A bar chart with bars at x positions 1 to 4
B. A line plot connecting points (1,10), (2,20), (3,15), (4,25) with circle markers
C. A scatter plot with only circle markers at given points
D. An error because marker='o' is invalid
Solution
Step 1: Understand plt.plot with marker
The code plots a line connecting points with circle markers at each point.
Step 2: Differentiate from scatter and bar plots
plt.plot with marker='o' adds markers on a line plot, not a scatter or bar chart.
Final Answer:
A line plot connecting points (1,10), (2,20), (3,15), (4,25) with circle markers -> Option B
Quick Check:
plt.plot + marker='o' = line with circles [OK]
Hint: plt.plot with marker='o' adds circle markers on line [OK]
Common Mistakes:
Thinking plt.plot with marker='o' creates scatter plot
Confusing bar chart with line plot
Assuming marker='o' causes error
4. Identify the error in this code that tries to plot two lines on the same graph: