Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 寻找两条等高线的交点坐标_Python_Matplotlib_Coordinates_Intersection_Contour - Fatal编程技术网

Python 寻找两条等高线的交点坐标

Python 寻找两条等高线的交点坐标,python,matplotlib,coordinates,intersection,contour,Python,Matplotlib,Coordinates,Intersection,Contour,使用 作为指导,我收到以下错误消息(我的代码在消息下面): 这是一段有问题的代码,它只使用了普通轮廓,并给出了“IndexError:list index out-range”,这也是我在使用未定义参数调用findIntersection时得到的结果,这有点奇怪。谢谢你的提示 我想出了一个有点模棱两可的答案,所以请告诉我是否有办法在3D中完成这一切。 软糖是做一个2D绘图来获得我的交点坐标,然后我做一个3D绘图并使用我在2D绘图中找到的东西(参见Jupyter笔记本代码) 这给出了下面的图像

使用 作为指导,我收到以下错误消息(我的代码在消息下面):

这是一段有问题的代码,它只使用了普通轮廓,并给出了“IndexError:list index out-range”,这也是我在使用未定义参数调用findIntersection时得到的结果,这有点奇怪。谢谢你的提示


我想出了一个有点模棱两可的答案,所以请告诉我是否有办法在3D中完成这一切。 软糖是做一个2D绘图来获得我的交点坐标,然后我做一个3D绘图并使用我在2D绘图中找到的东西(参见Jupyter笔记本代码)

这给出了下面的图像

三维代码为

def fhyp(x, y):
    return ((x - 5) * (y - 5) - 25)

x = np.linspace(0, 21, 20)
y = np.linspace(0, 21, 20)

X, Y = np.meshgrid(x, y)
Z = fhyp(X, Y)

def fcirc(x, y):
    return (x**2 + y**2 - 400)

x = np.linspace(0, 21, 20)
y = np.linspace(0, 21, 20)

X, Y = np.meshgrid(x, y)
Z2 = fcirc(X, Y)
fig = plt.figure()
fig.set_size_inches(18.5, 10.5)   #, forward=True)
ax = plt.axes(projection='3d')
x = np.linspace(0, 21, 20)
y = np.linspace(0, 21, 20)
X, Y = np.meshgrid(x, y)

ax.contour3D(X, Y, Z, 1, cmap='winter') #4th parm was 29 to give 29 contour lines
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');
#
ax.contour3D(X, Y, Z2, 1, cmap='autumn') 
ax.set_title('Contour3D: Using meshgrid X Y ')


from scipy import optimize

def f(p):
    x, y = p
    e1 = ((x - 5) * (y - 5) - 25)
    e2 = (x**2 + y**2 - 400)
return e1, e2

x2, y2 = optimize.fsolve(f, (5, 5))

zval = ((x2 - 5) * (y2 - 5) - 25)

print(x2,y2, zval)
vals = [x2,y2,zval]
result = np.array(vals)
print(result)

plt.plot([result[0]],[result[1]],[result[2]], "rx")

for i in range(5):
    x = [18.80452816482531,18.80452816482531]
    y = [6.810999963323182, 6.810999963323182]
    z = [-400,400]
plt.plot(x,y,z,'k--',alpha=0.8, linewidth=0.5)

附录:Z是一个列表列表(我在PyCharm中检查过)-每个子列表都是一组20个浮点数,因此我认为可以将Z限定为2D数组。事实上,Z、X和Y是列表列表列表,作为一个矩阵,将是20x20个矩阵
fig = plt.figure()
fig.set_size_inches(18.5, 10.5)   #, forward=True)
ax = plt.axes(projection='3d')
x = np.linspace(0, 21, 20)
y = np.linspace(0, 21, 20)
X, Y = np.meshgrid(x, y)

ax.contour3D(X, Y, ((X - 5) * (Y - 5) - 25), 29, cmap='winter')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');
#fig.add_subplot(ax.contour3D(X, Y, Z2, 70, cmap='winter')) #binary'))
ax.contour3D(X, Y, (X**2 + Y**2 - 400), 29, cmap='autumn')
ax.set_title('Contour3D: Using meshgrid X Y ')
from shapely import geometry

def findIntersection(contour1,contour2):
  p1 = contour1.collections[0].get_paths()[0]
  v1 = p1.vertices

  p2 = contour2.collections[0].get_paths()[0]
  v2 = p2.vertices

  poly1 = geometry.LineString(v1)
  poly2 = geometry.LineString(v2)

  intersection = poly1.intersection(poly2)

  return intersection

figtst2 = plt.figure()
figtst2.set_size_inches(18.5, 10.5)   #, forward=True)
ax2 = plt.axes(projection='3d')
c1 = ax2.contour(X,Y,((X - 5) * (Y - 5) - 25),1,colors='green', linewidths=3)
c2 = ax2.contour(X,Y,(X**2 + Y**2 - 400),1,colors='orange', linewidths=3)

ax2.set_title('Contour Using meshgrid X Y & looking for intersections')



intersection_example = findIntersection(c1,c2)

# where coordinates can be accessed by

intersection_example.x ##get x points
intersection_example.y ##get y points
list(intersection_example.coords)  ## get in [x,y] formatting
from shapely import geometry
fig2 = plt.figure()
fig2.set_size_inches(18.5, 10.5)   #, forward=True)
#plt.axes(projection='3d')  # 3D gives no vertices "list index out of range"

tau = np.arange(0,23,1)

x,y= np.meshgrid(tau,tau)
cs = plt.contour(x, y, np.array((x - 5) * (y - 5) - 25), [1],colors='k')
cs2 = plt.contour(x, y, np.array((x**2 + y**2 - 400)), [1],colors='k')  #x**2 + y**2 - 400



from shapely.geometry import LineString
v1 = cs.collections[0].get_paths()[0].vertices
v2 = cs2.collections[0].get_paths()[0].vertices

ls1 = LineString(v1)
ls2 = LineString(v2)
points = ls1.intersection(ls2)
print('points', np.array(points))
def fhyp(x, y):
    return ((x - 5) * (y - 5) - 25)

x = np.linspace(0, 21, 20)
y = np.linspace(0, 21, 20)

X, Y = np.meshgrid(x, y)
Z = fhyp(X, Y)

def fcirc(x, y):
    return (x**2 + y**2 - 400)

x = np.linspace(0, 21, 20)
y = np.linspace(0, 21, 20)

X, Y = np.meshgrid(x, y)
Z2 = fcirc(X, Y)
fig = plt.figure()
fig.set_size_inches(18.5, 10.5)   #, forward=True)
ax = plt.axes(projection='3d')
x = np.linspace(0, 21, 20)
y = np.linspace(0, 21, 20)
X, Y = np.meshgrid(x, y)

ax.contour3D(X, Y, Z, 1, cmap='winter') #4th parm was 29 to give 29 contour lines
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');
#
ax.contour3D(X, Y, Z2, 1, cmap='autumn') 
ax.set_title('Contour3D: Using meshgrid X Y ')


from scipy import optimize

def f(p):
    x, y = p
    e1 = ((x - 5) * (y - 5) - 25)
    e2 = (x**2 + y**2 - 400)
return e1, e2

x2, y2 = optimize.fsolve(f, (5, 5))

zval = ((x2 - 5) * (y2 - 5) - 25)

print(x2,y2, zval)
vals = [x2,y2,zval]
result = np.array(vals)
print(result)

plt.plot([result[0]],[result[1]],[result[2]], "rx")

for i in range(5):
    x = [18.80452816482531,18.80452816482531]
    y = [6.810999963323182, 6.810999963323182]
    z = [-400,400]
plt.plot(x,y,z,'k--',alpha=0.8, linewidth=0.5)