【问题标题】:How to connect the points in such a way that a parallelogram is created如何以创建平行四边形的方式连接点
【发布时间】:2021-11-08 19:05:34
【问题描述】:

我的df 表中有四个坐标:

lat1 lon1 lat2 lon2 lat3 lon3 lat4 lon4
51.071833 6.237204 51.071836 6.237195 51.071833 6.237195 51.071836 6.237204

根据这些数据,我尝试以创建平行四边形的方式连接点。

散点图:

# selecting columns start with 'lat'
xx = df[[col for col in test if col.startswith('lat')]].stack().to_list() 
# selecting columns start with 'lon'
yy = df[[col for col in test if col.startswith('lon')]].stack().to_list() 

plt.scatter(xx,yy)

不幸的是,当我尝试用线连接点时,它不起作用:

plt.scatter(xx,yy)
plt.plot(xx,yy)

预期结果:

我猜是因为积分没有排序。我也试过这个:

points = np.c_[xx, yy]

from sklearn.neighbors import NearestNeighbors
clf = NearestNeighbors(2).fit(points)
G = clf.kneighbors_graph()

import networkx as nx
T = nx.from_scipy_sparse_matrix(G)

order = list(nx.dfs_preorder_nodes(T, 0))

xx = xx[order]
yy = yy[order]

plt.plot(xx, yy)
plt.show()

但还是不正确。

你知道问题出在哪里吗?

谢谢

【问题讨论】:

    标签: python matplotlib geometry coordinates


    【解决方案1】:

    可以计算出四个点的convex hull

    import numpy as np
    points=np.column_stack((xx,yy))
    
    from scipy.spatial import ConvexHull
    hull = ConvexHull(points)
    for simplex in hull.simplices:
        plt.plot(points[simplex, 0], points[simplex, 1], 'k-')
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 2010-11-16
      • 2010-09-19
      • 2014-11-17
      • 2019-12-14
      • 1970-01-01
      相关资源
      最近更新 更多