0
0
Matplotlibdata~30 mins

Trend lines on scatter plots in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Trend lines on scatter plots
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use plt.scatter for points, plt.plot for the line, and plt.show() to display.