0

I wanted to have the intersections of two lines using numpy. The link that has introduced to me says how to reach the intersections but I got new error of

TypeError: only integer scalar arrays can be converted to a scalar index

The corrected code based on my dataset is below:

import numpy as np
import matplotlib.pyplot as plt

with open('configuration_300.out', 'r') as f:
   lines = f.readlines()
   x_1 = [float(line.split()[0]) for line in lines]
   y_1 = [float(line.split()[1]) for line in lines]

   g = [x_1, y_1]

## size of x_1 and y_1 is 1696
x_2 = np.zeros(1696)
y_2 = np.ones(1696)

h = [x_2,y_2]

def find_roots(g,h):
   s = np.abs(np.diff(np.sign(h))).astype(float)
   return g[:-1][s] + np.diff(g)[s]/(np.abs(h[1:][s]/g[:-1][s])+1)

z = find_roots(g,h)

import matplotlib.pyplot as plt

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

plt.show()

After running I got the error which was aforementioned

All helps will be appreciated

Hossein Amini
  • 35
  • 2
  • 9
  • can you post few lines of you data and expected plot result? – Zaraki Kenpachi Feb 26 '20 at 14:02
  • this could be the problem, s = np.abs(np.diff(np.sign(h))).astype(float). you used s as index, it should be int not float – Aly Hosny Feb 26 '20 at 14:04
  • 2
    Compared to the [original answer](https://stackoverflow.com/a/46911822/4124317) you changed `.astype(bool)` to `.astype(float)`. That makes no sense. Restore the original. – ImportanceOfBeingErnest Feb 26 '20 at 14:07
  • @ImportanceOfBeingErnest even if do not change to float and keep it bool, nothing is gonna happened. Same as non-zero root, I have tried to get results, but still I get the same error.\ – Hossein Amini Feb 26 '20 at 14:21
  • 1
    Try to understand the original answer. It takes one x array and one y array as input. Here you put something entirely different in. – ImportanceOfBeingErnest Feb 26 '20 at 14:29
  • @HosseinAmini The posted code doesn't make much sense. The [original code](https://stackoverflow.com/a/46911822/4124317) is meant to find the zeros of a 1D function given a list of x and of y coordinates (sorted on x). Your code gives it 2 2D arrays, of which the second is all zeros. Using this second array to find sign changes is futile. You don't show nor explain how your x_1 and y_1 look like. From the intro one could guess that it are two curves, which a common (but unknown) x-axis. – JohanC Feb 26 '20 at 21:53
  • @HosseinAmini .... If so, you could call find_root with the unknown x as first parameter and `y_1- x_1` as second. Anyway, you should convert your x_1 and y_1 to numpy arrays (`x_1 = np.array(x_1)`). Also, if you copy someone's code, it is not very polite not to mention the source. – JohanC Feb 26 '20 at 21:53

1 Answers1

0

To solve this problem, I used this way and it was good and fast enough.

import numpy as np
import matplotlib.pyplot as plt
%matplotlib qt

with open('file_300.out', 'r') as f:
    lines = f.readlines()
    x = [float(line.split()[0]) for line in lines]
    y = [float(line.split()[1]) for line in lines]

xx = []
for i in range(1,len(x)):
    if (y[i] > 1 and y[i-1] < 1) or (y[i] < 1 and y[i-1] > 1):
        xx.append((x[i]+x[i-1])/2)

yx = [0 for _ in range(len(xx))]


plt.axhline(y=1, color='g', linestyle='-') 
plt.plot(x,y)
plt.plot(xx,yx, color="C2", marker="o", ls="", ms=5)
plt.axis('equal')
Hossein Amini
  • 35
  • 2
  • 9