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! :)
JavaScript
x
18
18
1
!pip install matplotlib==3.4
2
3
%matplotlib inline
4
import matplotlib.pyplot as plt
5
6
x = [0,2,4,6,8,10,12,14.3,16.2,18,20.5,22.2,25.1,
7
26.1,28,30,33.3,34.5,36,38,40]
8
y = [13.4,23.7,35.1,48.3,62.7,76.4,91.3,106.5,119.6,131.3,
9
146.9,157.3,173.8,180.1,189.4,199.5,215.2,220.6,227,234.7,242.2]
10
slope = (131.3-119.6)/(18-16.2)
11
plt.figure(figsize=(10, 5))
12
plt.axline((16.2,119.6), slope = slope, linestyle = '--', color = 'r')
13
plt.grid()
14
plt.minorticks_on()
15
plt.axvline(30,linestyle = '--', color = 'black')
16
plt.plot(x,y, linewidth = 2.5)
17
plt.show()
18
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.
JavaScript
1
21
21
1
import matplotlib.pyplot as plt
2
3
x = [0,2,4,6,8,10,12,14.3,16.2,18,20.5,22.2,25.1,
4
26.1,28,30,33.3,34.5,36,38,40]
5
y = [13.4,23.7,35.1,48.3,62.7,76.4,91.3,106.5,119.6,131.3,
6
146.9,157.3,173.8,180.1,189.4,199.5,215.2,220.6,227,234.7,242.2]
7
slope = (131.3-119.6)/(18-16.2)
8
plt.figure(figsize=(10, 5))
9
10
point_1 = (16.2, 119.6)
11
q = point_1[1] - slope * point_1[0]
12
x2 = 30
13
point_2 = (x2, slope * x2 + q)
14
plt.axline(point_1, slope = slope, linestyle = '--', color = 'r')
15
plt.grid()
16
plt.minorticks_on()
17
plt.axvline(x2,linestyle = '--', color = 'black')
18
plt.plot(x,y, linewidth = 2.5)
19
plt.scatter(*point_2, color="g", s=100)
20
plt.show()
21