0% found this document useful (0 votes)
184 views

Python - How To Find The Exact Intersection of A Curve (As NP - Array) With y 0 - Stack Overflow

The document discusses finding the exact point of intersection between a curve defined by numpy arrays and the y-axis (or any other value on the y-axis). It provides a solution using simple linear interpolation to find the roots or zeros of a numpy array, and demonstrates plotting the intersection points. The method can also be used to find the intersection between two curves by finding the roots of the difference between the two arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
184 views

Python - How To Find The Exact Intersection of A Curve (As NP - Array) With y 0 - Stack Overflow

The document discusses finding the exact point of intersection between a curve defined by numpy arrays and the y-axis (or any other value on the y-axis). It provides a solution using simple linear interpolation to find the roots or zeros of a numpy array, and demonstrates plotting the intersection points. The method can also be used to find the intersection between two curves by finding the roots of the difference between the two arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

 

How to find the exact intersection of a curve (as np.array) with


y==0?
Asked
4 years, 7 months ago Modified
2 years, 3 months ago Viewed
4k times

How can I get from a plot in Python an exact value on y - axis? I have two arrays
vertical_data and gradient(temperature_data) and I plotted them as:
7
plt.plot(gradient(temperature_data),vertical_data)

plt.show()

8 Plot shown here:

I need the zero value but it is not exactly zero, it's a float.

python numpy matplotlib graph

Your Share
privacyImprove this question edited Feb 26, 2020 at 22:54 asked Oct 24, 2017 at 11:28
By clicking
Follow“Accept all cookies”, you agree Stack Exchange
JohanCcan store cookies on your device
Arcturus
and disclose
information in accordance with our Cookie Policy. 59.3k 8 19 45 73 1 3

Accept all cookies Customize settings


I guess the problem description is a bit misleading, if present at all. What you want is to find the zero of
a numpy array. This has nothing to do with matplotlib. (Also there are more than one zero). This is in
general a non-trivial task. But depending on the accuracy needed, can be simplified.
– ImportanceOfBeingErnest
Oct 24, 2017 at 12:27

Well I should see it in that plotted graph, I think


–  Arcturus
Oct 24, 2017 at 15:04

Sorted by:
1 Answer
Highest score (default)

I did not find a good answer to the question of how to find the roots or zeros of a numpy
array, so here is a solution, using simple linear interpolation.
16
import numpy as np

N = 750

x = .4+np.sort(np.random.rand(N))*3.5

y = (x-4)*np.cos(x*9.)*np.cos(x*6+0.05)+0.1

def find_roots(x,y):

s = np.abs(np.diff(np.sign(y))).astype(bool)

return x[:-1][s] + np.diff(x)[s]/(np.abs(y[1:][s]/y[:-1][s])+1)

z = find_roots(x,y)

import matplotlib.pyplot as plt

plt.plot(x,y)

plt.plot(z, np.zeros(len(z)), marker="o", ls="", ms=4)

plt.show()

Of course you can invert the roles of x and y to get

plt.plot(y,x)

plt.plot(np.zeros(len(z)),z, marker="o", ls="", ms=4)

Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose
information in accordance with our Cookie Policy.

Accept all cookies Customize settings


Because people where asking how to get the intercepts at non-zero values y0 , note that one
may simply find the zeros of y-y0 then.

y0 = 1.4

z = find_roots(x,y-y0)

# ...

plt.plot(z, np.zeros(len(z))+y0)

People were also asking how to get the intersection between two curves. In that case it's again
about finding the roots of the difference between the two, e.g.

x = .4 + np.sort(np.random.rand(N)) * 3.5

y1 = (x - 4) * np.cos(x * 9.) * np.cos(x * 6 + 0.05) + 0.1

y2 = (x - 2) * np.cos(x * 8.) * np.cos(x * 5 + 0.03) + 0.3

z = find_roots(x,y2-y1)

plt.plot(x,y1)

plt.plot(x,y2, color="C2")

plt.plot(z, np.interp(z, x, y1), marker="o", ls="", ms=4, color="C1")

Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose
information in accordance with our Cookie Policy.

Accept all cookies Customize settings


Share Improve this answer Follow edited Feb 26, 2020 at 23:00 answered Oct 24, 2017 at 13:28
ImportanceOfBeingErnes
t
289k 45 572 616

1 Hey, just wanted to say that's a neat idea to turn places where the sign of the function changes sign
into indices like this. Thanks!
– Dominik Stańczak
Oct 24, 2017 at 16:14

@JohanC For regularly spaced data one could replace np.diff(x)[s] by (x[1]-x[0]) . That would
give a very small performance gain, if that's what you mean.
– ImportanceOfBeingErnest
Feb 26, 2020 at
23:32

Why the last "+1" in return x[:-1][s] + np.diff(x)[s]/(np.abs(y[1:][s]/y[:-1][s])+1) ?


– catwith
Sep 13, 2021 at 6:09

@ImportanceOfBeingErnest Brilliant and elegant! Do you have a ref for the equation used / LaTeX
version? :)
– jtlz2
Apr 7 at 11:34

Also, not that it probably matters, but how (i) expensive and (ii) accurate (as in second-order residuals)
is this compared to other methods? Thanks again! :)
– jtlz2
Apr 7 at 11:35

Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose
information in accordance with our Cookie Policy.

Accept all cookies Customize settings

You might also like