[英]Find coordinates of intersection points between a density and a line in python
Once I have computed the pdf of my data, I'd like to extract the x coordinates of the two intersection points between the blue kernel and the black line as below (of type y=a). 计算完数据的pdf后,我想提取出蓝色核与黑线之间两个交点的x坐标,如下所示(类型y = a)。
import numpy as np; np.random.seed(10)
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(color_codes=True)
mean, cov = [0, 2], [(1, .5), (.5, 1)]
x, y = np.random.multivariate_normal(mean, cov, size=50).T
sns.kdeplot(x)
plt.axhline(y=0.15, color='black', linestyle='-')
sns.plt.show()
Ideally, I'd like a solution that wouldn't go though the plotting of a chart as I need to loop this for different values of y. 理想情况下,我想要一个不会通过图表绘制的解决方案,因为我需要针对不同的y值进行循环。
If you need and exact solution, you'll probably need to drop out of the programming world and derive the closed-form solution. 如果您需要准确的解决方案,则可能需要退出编程世界,并得出封闭形式的解决方案。
If a good approximation is all you need, simply interpolate. 如果您只需要一个很好的近似值,则只需插值即可。 You have the x and y coordinates for the plot.
您具有该图的x和y坐标。 Use these values to bracket the two solutions, and interpolate between the adjacent points.
使用这些值将两个解决方案括起来,并在相邻点之间进行插值。 I expect that a linear interpolation at the intersections shown will get you at least three digits of accuracy.
我希望在所示的相交处进行线性插值将使您至少获得三位数的精度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.