如何在matplotlib中找到axvline和axline之间交点的数值?
How to find the numeric value of the intersection point between an axvline and axline in matplotlib?
我有下面的代码,我需要找到axline和axvline之间的交点的数值,我不知道如何简单地解决这个问题,有人知道如何解决吗?提前无限感谢! :)
!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
首先,您需要找到斜线的方程,y=slope*x+q
:这很容易做到,因为您知道斜线的斜率和点。
接下来求解方程组:y=slope*x+q
、x=x0
(垂直线)。
这里我用绿色标记绘制了交点。
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()
我有下面的代码,我需要找到axline和axvline之间的交点的数值,我不知道如何简单地解决这个问题,有人知道如何解决吗?提前无限感谢! :)
!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
首先,您需要找到斜线的方程,y=slope*x+q
:这很容易做到,因为您知道斜线的斜率和点。
接下来求解方程组:y=slope*x+q
、x=x0
(垂直线)。
这里我用绿色标记绘制了交点。
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()