简体   繁体   中英

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).

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.

在此处输入图片说明

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM