In this guide, we will provide you with step-by-step instructions on how to create a Scatterplot with a regression line in Python. We will also provide you with a detailed explanation of the Scatterplot and regression line, and how they can be used to analyze data.
What is a Scatterplot with Regression Line
Scatterplot is a type of graph that is used to display the relationship between two variables. Each point on the Scatterplot represents a unique combination of the two variables being plotted. A regression line is a straight line that is used to approximate the relationship between the two variables. It can be used to make predictions about the value of one variable based on the value of the other variable.
Loading a Sample Data
import numpy as np
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.array([2, 3, 5, 6, 7, 8, 9, 10, 11, 13])
Method 1: Using Matplotlib
The code below demonstrates how to use Matplotlib to construct a scatterplot with an estimated regression line for these data:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.array([2, 3, 5, 6, 7, 8, 9, 10, 11, 13])
slope, intercept = np.polyfit(x, y, 1)
plt.scatter(x, y)
plt.plot(x, slope*x + intercept, color='red')
plt.title('Scatter Plot with Regression Line')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
Output:

This should generate a scatterplot with a regression line overlaid on it. You can adjust the data and formatting as needed for your specific use case.
Method 2: Using Seaborn
Another way to create a scatterplot with a regression line in Python is by using the Seaborn library and regplot()
function
Here’s how you can do it:
import seaborn as sns
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [2, 3, 5, 6, 7, 8, 9, 10, 11, 13]
sns.regplot(x=x, y=y, ci=None)
plt.title('Scatter Plot with Regression Line')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
Output:

In this example, the regplot()
function takes the x and y values as arguments and automatically plots a scatterplot with a linear regression line overlaid on it.
Note that the ci=None option instructs Seaborn to conceal the confidence interval bands from the plot. You may choose to reveal them if you wish, however:
import seaborn as sns
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [2, 3, 5, 6, 7, 8, 9, 10, 11, 13]
sns.regplot(x=x, y=y, ci=95)
plt.title('Scatter Plot with Regression Line')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
Output:

In this example, the ci
parameter is set to “95”, which specifies that the confidence interval lines should be drawn at 95% confidence intervals. You can adjust the value of ci
to specify a different confidence level as needed.
This should generate a scatterplot with a regression line and confidence interval lines overlaid on it using Seaborn. You can adjust the data and formatting as needed for your specific use case.
Wrap up
Creating a Scatterplot with a regression line in Python is a simple and straightforward process. You can use either the regplot()
or lmplot()
function from the Seaborn visualization library to create a Scatterplot with a regression line overlaid on it.
To add confidence interval lines to the plot, you can use the ci
parameter in the lmplot()
function and set it to “sd” or “boot” to specify the type of confidence interval. Alternatively, you can use the ci
parameter in the regplot()
function and set it to the desired confidence level as a percentage.
By adjusting the data and formatting as needed, you can create professional-quality Scatterplots with regression lines and confidence interval lines in Python for your data analysis and visualization needs.
To learn more about Scatterplot with a Regression check out the:
https://en.wikipedia.org/wiki/Scatter_plot
Thanks for reading. Happy coding!