Table of Contents
The process of adding a line to a scatter plot using Seaborn involves using the “regplot” function, which creates a linear regression line on the scatter plot. This allows for visualizing the relationship between two variables and identifying any potential trends or patterns. The “regplot” function also allows for customizations such as changing the line color and style. Additionally, Seaborn offers various other plot types and functions that can be used to enhance the scatter plot, such as adding confidence intervals or incorporating categorical variables. Overall, incorporating a line to a scatter plot using Seaborn is a useful tool for exploring and analyzing data in a visually appealing manner.
Add Line to Scatter Plot in Seaborn
You can use the following methods to add a line to a scatter plot in seaborn:
Method 1: Add Horizontal Line
#add horizontal line at y=15 plt.axhline(y=15)
Method 2: Add Vertical Line
#add vertical line at x=4 plt.axvline(x=4)
Method 3: Add Custom Line
#add straight line that extends from (x,y) coordinates (2,0) to (6, 25) plt.plot([2, 6], [0, 25])
The following examples show how to use each method in practice.
Example 1: Add Horizontal Line to Seaborn Scatter Plot
The following code shows how to create a scatter plot in seaborn and add a horizontal line at y = 15:
import seaborn as sns
import matplotlib.pyplotas plt
#create DataFrame
df = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6, 7, 8],
'y': [18, 22, 19, 14, 14, 11, 20, 28]})
#create scatterplot
sns.scatterplot(x=df.x, y=df.y)
#add horizontal line to scatterplot
plt.axhline(y=15)

Example 2: Add Vertical Line to Seaborn Scatter Plot
The following code shows how to create a scatter plot in seaborn and add a vertical line at x = 4:
import seaborn as sns
import matplotlib.pyplotas plt
#create DataFrame
df = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6, 7, 8],
'y': [18, 22, 19, 14, 14, 11, 20, 28]})
#create scatterplot
sns.scatterplot(x=df.x, y=df.y)
#add vertical line to scatterplot
plt.axvline(x=4)
Example 3: Add Custom Line to Seaborn Scatter Plot
import seaborn as sns
import matplotlib.pyplotas plt
#create DataFrame
df = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6, 7, 8],
'y': [18, 22, 19, 14, 14, 11, 20, 28]})
#create scatterplot
sns.scatterplot(x=df.x, y=df.y)
#add custom line to scatterplot
plt.plot([2, 6], [0, 25])

Note: You can find the complete documentation to the seaborn scatter() function .
The following tutorials explain how to perform other common tasks using seaborn:
Cite this article
stats writer (2024). How can I add a line to a scatter plot using Seaborn?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-a-line-to-a-scatter-plot-using-seaborn/
stats writer. "How can I add a line to a scatter plot using Seaborn?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-a-line-to-a-scatter-plot-using-seaborn/.
stats writer. "How can I add a line to a scatter plot using Seaborn?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-a-line-to-a-scatter-plot-using-seaborn/.
stats writer (2024) 'How can I add a line to a scatter plot using Seaborn?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-a-line-to-a-scatter-plot-using-seaborn/.
[1] stats writer, "How can I add a line to a scatter plot using Seaborn?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I add a line to a scatter plot using Seaborn?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
