I have a probably simple question, that keeps me going already for quiet a while. Is there a simple way to return the intersection of two plotted (non-analytical) datasets in python matplotlib ?
For elaboration, I have something like this:
x=[1.4,2.1,3,5.9,8,9,23]
y=[2.3,3.1,1,3.9,8,9,11]
x1=[1,2,3,4,6,8,9]
y1=[4,12,7,1,6.3,8.5,12]
plot(x1,y1,'k-',x,y,'b-')
The data in this example is totaly arbitrary. I would now like to know if there is a simple build in function that I keep missing, that returns me the precise intersections between the two plots.
Hope I made myself clear, and also that I didnt miss something totaly obvious...
We could use
scipy.interpolate.PiecewisePolynomial
to create functions which are defined by your piecewise-linear data.We could then take the difference of these two functions,
and use optimize.fsolve to find the roots of
pdiff
:yields
The first column is the x-value, the second column is the y-value of the first PiecewisePolynomial evaluated at
x
, and the third column is the y-value for the second PiecewisePolynomial.