[英]Finding intersection co-ordinates of 2 contours
使用“在 Python 中查找兩個等高線圖的交點”作為指導,我收到以下錯誤消息(代碼如下):
<ipython-input-98-993ccd512742> in <module>
9
10
---> 11 intersection_example = findIntersection(c1,c2)
12
<ipython-input-97-995cbb9fd0d0> in findIntersection(contour1, contour2)
2
3 def findIntersection(contour1,contour2):
----> 4 p1 = contour1.collections[0].get_paths()[0]
5 v1 = p1.vertices
6
IndexError: list index out of range
下面的第一個代碼示例給了我一個 3D 輪廓 plot 沒有錯誤:
import matplotlib.pyplot as plt
import numpy as np
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 ')
以上產生:
下一個示例是使用contour
(而不是輪廓contour3D
)並導致錯誤的問題代碼段:
IndexError:列表索引超出范圍
這也是使用未定義參數調用findIntersection
時產生的錯誤。
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')
# Error is generated on the next line
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
繪制ax2
會產生:
注意:如果我使用線性空間x
和y
而不是網格X
和Y
我得到:
TypeError:輸入 z 必須是二維數組。
我開發了一個有點軟糖的答案,所以請告訴我是否有辦法在 3D 中完成這一切。 The fudge is to do a 2D plot to get my intersection coordinates, then I do a 3D plot and use what I found in the 2D plot (see Jupyter notebook code )
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))
3D 代碼是;
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.