此shapely错误消息有什么问题

2024-06-16 11:20:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为Rsize的字典,它以数字列表作为键值对。字典是这样的

{10: [0.6621485767296484, 0.6610747762560114, 0.659607022086639, 0.6567761845867727, 0.6535392433801197, 0.6485977028504701, 0.6393024556394106, 0.6223866436257335, 0.5999232392636733, 0.5418403536642005, 0.4961461379219235, 0.4280278015788386, 0.35462315989740956, 0.2863017237662875, 0.2312185739351389, 0.18306363413831017], 12: [0.6638977494825118, 0.663295576452323, 0.662262804664348, 0.6610413916318628, 0.6590939627030634, 0.655212304186114, 0.6492141689834672, 0.6380632834031537, 0.6096663492242224, 0.5647498006858608, 0.4983281599318278, 0.3961350546063216, 0.32119092575707087, 0.2257230704567207, 0.1816695139119151, 0.14363448808684576], 14: [0.6649598494971014, 0.6644370245269158, 0.6638578972784479, 0.6630511299276417, 0.6615070373022596, 0.6596206155163766, 0.6560628158033714, 0.6487119276511941, 0.6343385358239866, 0.5792725000508062, 0.49799837531709923, 0.42482204326408324, 0.26633662071414366, 0.2028085235063155, 0.12411214668987203, 0.09336935548451253]}
[0.6621485767296484, 0.6610747762560114, 0.659607022086639, 0.6567761845867727, 0.6535392433801197, 0.6485977028504701, 0.6393024556394106, 0.6223866436257335, 0.5999232392636733, 0.5418403536642005, 0.4961461379219235, 0.4280278015788386, 0.35462315989740956, 0.2863017237662875, 0.2312185739351389, 0.18306363413831017]


钥匙是10、14、16。我使用了每个列表进行绘图,并希望找出它们的成对交点。我已经为此编写了以下脚本,并使用shapely相交函数检测交点

from shapely.geometry import LineString
Rsize={10: [0.6621485767296484, 0.6610747762560114, 0.659607022086639, 0.6567761845867727, 0.6535392433801197, 0.6485977028504701, 0.6393024556394106, 0.6223866436257335, 0.5999232392636733, 0.5418403536642005, 0.4961461379219235, 0.4280278015788386, 0.35462315989740956, 0.2863017237662875, 0.2312185739351389, 0.18306363413831017], 12: [0.6638977494825118, 0.663295576452323, 0.662262804664348, 0.6610413916318628, 0.6590939627030634, 0.655212304186114, 0.6492141689834672, 0.6380632834031537, 0.6096663492242224, 0.5647498006858608, 0.4983281599318278, 0.3961350546063216, 0.32119092575707087, 0.2257230704567207, 0.1816695139119151, 0.14363448808684576], 14: [0.6649598494971014, 0.6644370245269158, 0.6638578972784479, 0.6630511299276417, 0.6615070373022596, 0.6596206155163766, 0.6560628158033714, 0.6487119276511941, 0.6343385358239866, 0.5792725000508062, 0.49799837531709923, 0.42482204326408324, 0.26633662071414366, 0.2028085235063155, 0.12411214668987203, 0.09336935548451253]}
[0.6621485767296484, 0.6610747762560114, 0.659607022086639, 0.6567761845867727, 0.6535392433801197, 0.6485977028504701, 0.6393024556394106, 0.6223866436257335, 0.5999232392636733, 0.5418403536642005, 0.4961461379219235, 0.4280278015788386, 0.35462315989740956, 0.2863017237662875, 0.2312185739351389, 0.18306363413831017]

listkT = np.arange(4.0,4.8,0.05)

print(Rsize[10])

plt.figure(figsize=(18, 10))
plt.title ('Binder cumulant for critical point')

plt.plot(listkT, Rsize[10], '-',label='Lattice sie 10')
plt.plot(listkT, Rsize[12], '-',label='Lattice sie 12')
plt.plot(listkT, Rsize[14], '-',label='Lattice sie 14')
plt.legend() 
plt.show()

curve_10=LineString(np.column_stack((listkT, Rsize[10])))
curve_12=LineString(np.column_stack((listkT, Rsize[12])))
curve_14=LineString(np.column_stack((listkT, Rsize[14])))

intersection12 = curve_10.intersection(curve_12)
intersection14 = curve_10.intersection(curve_14)

plt.plot(*LineString(intersection12).xy, 'o')
plt.plot(*LineString(intersection14).xy, 'o')

x12, y = LineString(intersection12).xy
x14, y = LineString(intersection14).xy
print(np.intersect1d(x12, x14))
print(x12,x14)

但shapely抛出了一个断言错误


  File "C:\Users\Endeavour\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\Endeavour\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
  File "E:/Project/Codes/3D.py", line 118, in <module>
    plt.plot(*LineString(intersection12).xy, 'o')

  File "C:\Users\Endeavour\Anaconda3\lib\site-packages\shapely\geometry\linestring.py", line 48, in __init__
    self._set_coords(coordinates)

  File "C:\Users\Endeavour\Anaconda3\lib\site-packages\shapely\geometry\linestring.py", line 97, in _set_coords
    ret = geos_linestring_from_py(coordinates)

  File "shapely/speedups/_speedups.pyx", line 87, in shapely.speedups._speedups.geos_linestring_from_py

AssertionError

但是,matplotlib正确绘制了这些图。 我第一次使用shapely,之前没有这方面的经验。任何帮助都将不胜感激。非常感谢。 注:最终目标是获得3条曲线的交点。如果没有交叉点,找到它们最接近的点就足够了。任何建议或图书馆的功能,以找到这将是很大的帮助

先谢谢你


Tags: inpyplotnplinepltusersfile
1条回答
网友
1楼 · 发布于 2024-06-16 11:20:13

在断言错误之后,我检查了shapely/speedups/_speedups.pyxline 87geos_linestring_from_py函数要求您传递一个LineString或一个LinearRing。当我打印你的intersection12intersection14时,我得到:

POINT (4.503201814825258 0.4917840919384173)
POINT (4.51830999373466 0.4712012116887737)

因此,您正在传递一个Point实例来创建一个LineString,它创建了一个断言错误

除了错误之外,您的方法也是错误的,因为它假定(1)两条曲线之间有多个交点,(2)三条曲线相交处有一个绝对点。如果放大绘图,可以看到两者都不是这样

enter image description here

红色圆圈对应于你的intersection12,紫色圆圈对应于intersection14。如果您正在寻找近似解决方案,可能在这种情况下,查找这些点的平均值会有所帮助,但对于每对曲线具有多个交点的更复杂曲线,也不建议这样做

相关问题 更多 >