Skip to content
Advertisement

How to find the numeric value of the intersection point between an axvline and axline in matplotlib?

I have the code below and I needed to find the numerical value of the intersection point between the axline and the axvline, I have no idea how to solve this in a simple way, does anyone know how to solve it? Infinite thanks in advance! :)

!pip install matplotlib==3.4

%matplotlib inline  
import matplotlib.pyplot as plt 

x = [0,2,4,6,8,10,12,14.3,16.2,18,20.5,22.2,25.1,
     26.1,28,30,33.3,34.5,36,38,40]
y = [13.4,23.7,35.1,48.3,62.7,76.4,91.3,106.5,119.6,131.3,
     146.9,157.3,173.8,180.1,189.4,199.5,215.2,220.6,227,234.7,242.2]
slope = (131.3-119.6)/(18-16.2)
plt.figure(figsize=(10, 5))
plt.axline((16.2,119.6), slope = slope, linestyle = '--', color = 'r')
plt.grid()
plt.minorticks_on()
plt.axvline(30,linestyle = '--', color = 'black')
plt.plot(x,y, linewidth = 2.5)
plt.show()

Plot Result

Advertisement

Answer

First, you need to find the equation of the oblique line, y=slope*x+q: that’ easy to do, since you know the slope and a point of the line.

Next you solve the system of equations: y=slope*x+q, x=x0 (the vertical line).

Here I plotted the intersection point with a green marker.

import matplotlib.pyplot as plt 

x = [0,2,4,6,8,10,12,14.3,16.2,18,20.5,22.2,25.1,
     26.1,28,30,33.3,34.5,36,38,40]
y = [13.4,23.7,35.1,48.3,62.7,76.4,91.3,106.5,119.6,131.3,
     146.9,157.3,173.8,180.1,189.4,199.5,215.2,220.6,227,234.7,242.2]
slope = (131.3-119.6)/(18-16.2)
plt.figure(figsize=(10, 5))

point_1 = (16.2, 119.6)
q = point_1[1] - slope * point_1[0]
x2 = 30
point_2 = (x2, slope * x2 + q)
plt.axline(point_1, slope = slope, linestyle = '--', color = 'r')
plt.grid()
plt.minorticks_on()
plt.axvline(x2,linestyle = '--', color = 'black')
plt.plot(x,y, linewidth = 2.5)
plt.scatter(*point_2, color="g", s=100)
plt.show()
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement