Course contentsShow
← Python
Lesson 980 of 1,06015. Data Science FundamentalsFree lesson

Revealing Relationships with Scatter and Regression Plots

Identify correlations between continuous variables by plotting scatter points and overlaying linear regression models to highlight trends.

In this lesson, you will learn how to visualize the relationship between two continuous variables and quantify their linear trend using combined scatter and regression plots.

While histograms and box plots help us understand the distribution of single variables or categorical groups, they fall short when we need to see how two continuous variables move together. A scatter plot is our primary tool here: by placing one variable on the x-axis and another on the y-axis, we can immediately identify patterns, clusters, or outliers.

However, the human eye is often biased when interpreting "noise" in a scatter plot. This is where overlaying a linear regression model becomes essential. A regression line (often calculated using the method of least squares) creates a "best fit" path through your data, allowing you to see the direction and strength of the relationship at a glance.

Using the seaborn library, we can generate these plots with minimal syntax. By combining these layers, we move from merely "guessing" a correlation to visualizing a mathematically supported trend line.

import seaborn as sns
import matplotlib.pyplot as plt

# Visualizing the relationship between 'bill_amount' and 'tip_amount'
# The 'regplot' function automatically calculates and overlays the trend line
sns.regplot(data=tips_df, x='total_bill', y='tip', line_kws={'color': 'red'})

plt.title("Relationship between Bill Amount and Tips")
plt.show()

In this code, the scatter points show the raw data, while the regplot function computes the linear model and draws the trend line. If the line slopes upward, you have a positive correlation; if the points are tightly clustered around the line, the relationship is strong. If the points are widely dispersed, the linear trend is weak, regardless of the line's slope.

By layering these statistical visualizations, you gain the ability to communicate complex data dependencies clearly, turning abstract numerical correlations into intuitive visual evidence.

Key Takeaway: A scatter plot reveals the raw distribution of two continuous variables, while an overlaid regression line provides the mathematical context needed to interpret the strength and direction of their relationship.