📖 Scenario: You are a data analyst working with sales data. You want to see if there is a trend between the number of hours spent on advertising and the sales revenue.
🎯 Goal: Build a scatter plot showing advertising hours vs sales revenue and add a trend line to see the relationship clearly.
📋 What You'll Learn
Create a dictionary with advertising hours and sales revenue data
Create a variable for the degree of the polynomial for the trend line
Calculate the polynomial fit and generate the trend line values
Plot the scatter plot and the trend line using matplotlib
Display the plot
💡 Why This Matters
🌍 Real World
Trend lines help businesses see if more advertising hours lead to higher sales, guiding marketing decisions.
💼 Career
Data analysts and scientists often use scatter plots with trend lines to find patterns and make predictions.
Progress0 / 4 steps
1
DATA SETUP: Create the sales data dictionary
Create a dictionary called sales_data with these exact entries: 'hours': [1, 2, 3, 4, 5, 6, 7, 8] and 'revenue': [100, 150, 200, 250, 300, 350, 400, 450].
Matplotlib
Hint
Use a dictionary with keys 'hours' and 'revenue' and assign the exact lists as values.
2
CONFIGURATION: Set the polynomial degree for the trend line
Create a variable called degree and set it to 1 to indicate a linear trend line.
Matplotlib
Hint
Set degree to 1 for a straight line trend.
3
CORE LOGIC: Calculate the trend line values
Use numpy.polyfit with sales_data['hours'], sales_data['revenue'], and degree to get the polynomial coefficients. Then use numpy.poly1d to create a polynomial function called trend_line. Finally, create a list called trend_values by applying trend_line to sales_data['hours'].
Matplotlib
Hint
Use np.polyfit to get coefficients, then np.poly1d to create the function, then apply it to hours.
4
OUTPUT: Plot the scatter plot and trend line
Import matplotlib.pyplot as plt. Use plt.scatter to plot sales_data['hours'] vs sales_data['revenue']. Use plt.plot to plot sales_data['hours'] vs trend_values with a red line. Finally, call plt.show() to display the plot.
Matplotlib
Hint
Use plt.scatter for points, plt.plot for the line, and plt.show() to display.
Practice
(1/5)
1. What is the main purpose of adding a trend line to a scatter plot in matplotlib?
easy
A. To show the overall pattern or relationship between variables
B. To add colors to the scatter points
C. To change the size of scatter points
D. To remove outliers from the data
Solution
Step 1: Understand scatter plot purpose
A scatter plot shows individual data points but may not clearly show the overall trend.
Step 2: Role of trend line
A trend line summarizes the general direction or pattern in the data, making relationships easier to see.
Final Answer:
To show the overall pattern or relationship between variables -> Option A
Quick Check:
Trend line = overall pattern [OK]
Hint: Trend lines summarize data direction, not point details [OK]
Common Mistakes:
Thinking trend lines change point colors
Confusing trend lines with point size changes
Believing trend lines remove data points
2. Which of the following code snippets correctly fits a linear trend line to data arrays x and y using numpy?
easy
A. coefficients = np.polyfit(x, y, 1)
B. coefficients = np.polyfit(y, x, 1)
C. coefficients = np.polyfit(x, y, 2)
D. coefficients = np.polyfit(x, y)
Solution
Step 1: Understand np.polyfit parameters
np.polyfit(x, y, degree) fits a polynomial of given degree to x and y data.
Step 2: Correct usage for linear fit
For a linear trend line, degree = 1, and x is independent variable, y is dependent variable.
Final Answer:
coefficients = np.polyfit(x, y, 1) -> Option A
Quick Check:
np.polyfit(x, y, 1) fits linear trend [OK]
Hint: Use degree=1 for linear trend with np.polyfit(x, y, 1) [OK]
Common Mistakes:
Swapping x and y in np.polyfit
Omitting degree parameter
Using degree 2 for linear trend
3. Given the code below, what will be the output of print(trendline(3))?
import numpy as np
x = np.array([1, 2, 3, 4])
y = np.array([2, 4, 6, 8])
coefficients = np.polyfit(x, y, 1)
trendline = np.poly1d(coefficients)
print(trendline(3))
medium
A. Error due to incorrect polyfit usage
B. 5.0
C. 9.0
D. 6.0
Solution
Step 1: Analyze data and polyfit
Data y doubles x (2,4,6,8), so linear fit slope ~2 and intercept ~0.
Step 2: Calculate trendline at x=3
trendline(3) = 2*3 + 0 = 6.0
Final Answer:
6.0 -> Option D
Quick Check:
trendline(3) = 6.0 [OK]
Hint: Check if y is roughly 2*x; then trendline(3) = 6 [OK]
Common Mistakes:
Confusing x and y order in polyfit
Forgetting poly1d creates function
Assuming error instead of numeric output
4. Identify the error in the following code that tries to plot a scatter plot with a trend line:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1, 2, 3, 4])
y = np.array([2, 3, 5, 7])
coeff = np.polyfit(x, y, 1)
trend = np.poly1d(coeff)
plt.scatter(x, y)
plt.plot(x, trend)
plt.show()
medium
A. np.polyfit arguments are in wrong order
B. plt.plot(x, trend) should be plt.plot(x, trend(x))
C. plt.scatter should be called after plt.plot
D. Missing plt.xlabel and plt.ylabel calls
Solution
Step 1: Check trend line plotting
plt.plot expects y-values, but trend is a function, not values.
Step 2: Correct function call
Call trend(x) to get y-values for plotting: plt.plot(x, trend(x))
Final Answer:
plt.plot(x, trend) should be plt.plot(x, trend(x)) -> Option B
Quick Check:
Plot function needs y-values, not function [OK]
Hint: Call trend(x) to get y-values for plt.plot [OK]
Common Mistakes:
Passing function instead of values to plt.plot
Swapping x and y in polyfit
Thinking plot order affects output
Missing axis labels is not an error
5. You have noisy data with outliers. Which approach best improves the trend line fit on a scatter plot using matplotlib and numpy?
hard
A. Plot scatter without trend line to avoid confusion
B. Use np.polyfit with degree 1 on all data points
C. Remove outliers before fitting trend line with np.polyfit
D. Use plt.plot instead of plt.scatter for all points
Solution
Step 1: Understand effect of outliers
Outliers can distort the trend line fit, making it less representative.
Step 2: Improve fit by removing outliers
Removing or filtering outliers before fitting helps get a better trend line.
Final Answer:
Remove outliers before fitting trend line with np.polyfit -> Option C
Quick Check:
Remove outliers for better trend line [OK]
Hint: Filter outliers before fitting trend line for accuracy [OK]
Common Mistakes:
Fitting trend line on all data including outliers
Skipping trend line when data is noisy
Confusing plot types for trend line fitting
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.