Plotting Dual Lines of Best Fit and Their Intersection in Seaborn

Crafting a ‘Dogleg’ Plot with Dual Lines of Best Fit in Seaborn

In this comprehensive tutorial, we will delve into the intriguing realm of data visualization using Python’s Seaborn library. Our focus will be on creating a visually appealing plot known as a ‘dogleg’ plot, featuring two lines of best fit and determining their intersection point. This technique is invaluable for analyzing trends that exhibit directional changes or comparing distinct datasets.

What You’ll Learn

By the end of this guide, you will have acquired the skills to plot multiple regression lines simultaneously using Seaborn and calculate their intersection point. These capabilities are essential for conducting effective data analysis tasks with precision and accuracy.

Introduction to Problem and Solution

Navigating through complex data relationships demands a blend of creativity and technical expertise. Our scenario involves visualizing a ‘dogleg’ pattern, indicating a shift in trend necessitating the plotting of two distinct linear regressions. To address this challenge, we leverage the user-friendly Seaborn library known for its high-level interface and visually appealing plots.

To accomplish our goal, we first create a scatter plot using Seaborn to visualize our dataset. Subsequently, we fit two separate lines of best fit through relevant subsets of the data�one before and one after the ‘dogleg’. The most intricate part involves determining the intersection point where these two lines meet. This task requires basic algebraic calculations within Python to extract slope (m) and intercept (b) parameters from each line accurately.


import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.optimize import fsolve

# Sample Data Creation
x = np.arange(0, 20)
y = np.where(x < 10, x*2 + np.random.randn(*x.shape), x*1.5 + 10 + np.random.randn(*x.shape))

# Scatter Plot
sns.scatterplot(x=x, y=y)

# Line Fitting And Plotting 
slope1, intercept1 = np.polyfit(x[x<10], y[x<10], 1)
slope2, intercept2 = np.polyfit(x[x>=10], y[x>=10], 1)

plt.plot(x[x<10], slope1*x[x<10] + intercept1, color="blue")
plt.plot(x[x>=10], slope2*x[x>=10] + intercept2 , color="red")

def equations(p):
    x0,y0 = p
    return (y0 - slope1 * x0 - intercept1 , y0 - slope2 * x0 - intercept2)

intersection_point = fsolve(equations,(5,y[5]))

plt.plot(intersection_point[0], intersection_point[1], "go") # Marking Intersection Point

# Copyright PHD


By utilizing Seaborn for visualization and Matplotlib for plotting accuracy, we initiate with a scatter plot representing our dataset. Leveraging NumPy‘s polyfit method enables us to compute precise parameters�slope (m) and y-intercept (b)�for both segments pre- and post-‘dogleg’, which are then visually depicted on the scatter plot with distinctive colors.

The pivotal step involves identifying the intersection point where these regression lines converge. We define a function equations(p) encapsulating both line equations subtracted from each other; solving this system using fsolve from SciPy yields precise coordinates denoting their intersection highlighted on our plot.

Frequently Asked Questions

  • How do I install Seaborn? To install Seaborn, you can use pip install seaborn.

  • Can I use another optimization method instead of fsolve? Yes! Python offers various optimization tools like root from SciPy that can be used interchangeably based on your specific requirements.

  • Is it necessary to use NumPy’s polyfit? Could I manually calculate slopes? While manual calculation is feasible for simple cases, NumPy�s polyfit streamlines this process offering efficiency & accuracy especially beneficial for larger or complex datasets.

  • What if my data doesn’t neatly split into two segments? If clear segmentation is challenging, alternative methods such as smoothing splines might provide better insights when distinct categorization isn’t feasible.

  • Can I add more than two lines of best fit? Certainly! Extending beyond two segments increases complexity but principles discussed here can be adapted to accommodate additional lines ensuring accurate intersections are calculated between all pairs.


Mastering the art of plotting dual lines of best fit along with finding their intersection equips you with advanced analytical skills crucial for seasoned data analysts. This tutorial serves as a stepping stone towards exploring sophisticated analysis techniques and unlocking hidden insights within your datasets by delving deeper into mathematical foundations behind visualization methods.

Leave a Comment