问题描述
对于闭合曲线,我有(吨)个点的坐标,它们以x递增的顺序排序.
I have (tons of) coordinates of points for closed curve(s) sorted in x-increasing order.
以常规方式绘制时,我得到的结果是:
When plot it in the regular way the result i get is this:
(仅作为示例,以圆圈为好,我目前最多可以将其归类为变形虫)
(circle as an example only, the shapes I currently have can be, at best, classified as amoeboids)
但是我正在寻找的结果是这样的:
But the result I am looking for is something like this:
我已经查看了matplotlib,但找不到任何东西. (也许我的关键字输入有误...?)
I have looked through matplotlib and I couldn't find anything. (Maybe I had my keywords wrong...?)
我尝试通过以下方式重新格式化数据:
I have tried to reformat the data in the following ways:
-
随机选择一个点,先找到其最近的邻居,然后再找到下一个最近的邻居,依此类推.它在有时数据不太一致的边缘失效(最近的邻居可能在曲线的另一侧).
Pick a point at random, find its nearest neighbor and then the next nearest neighbor and so on..It fails at the edges where, sometimes the data isn't too consistent (the nearest neighbor maybe on the opposite side of the curve).
为解决数据不一致的问题,我试图检查两个点(被视为最近邻点)之间的坡度是否与先前连接的坡度匹配-失败,由于我找不到原因. (在我放弃之前花费了很多时间)
To account for inconsistent data, I tried to check if the slope between two points (which are being considered as nearest neighbors) matches with the previously connected slope - Fails, for reasons I could not find. (spent considerable number of hours before I gave up)
选择x_minimum和x_maximum(以及相应的y坐标),并绘制一条假想线,并在该线的任一侧对点进行排序. -当您的曲线看起来像香蕉时失败.
Pick x_minimum and x_maximum (and corresponding y coordinates) and draw an imaginary line and sort for points on either side of the line. - Fails when you have a curve that looks like a banana.
是否有可以帮助我到达所需位置的python软件包/库?还是可以帮助我提出一些想法,以便更好地对数据点进行排序?预先感谢.
Is there a python package/library that can help me get to where I want.? Or can you help me with ideas to sort my data points better.? Thanks in advance.
在我的圆上尝试过ConcaveHull,知道为什么这些线在某些地方重叠吗?这是图片:
Tried the ConcaveHull on the circle I had, any idea why the lines are overlapping at places.? Here's the image:
按照@Reblochon Masque在其答案的注释部分中所建议的那样,通过更改我的部分代码来解决该问题.
The problem was sorted out by changing part of my code as suggested by @Reblochon Masque in the comment section in his answer.
推荐答案
如果您不知道如何设置积分(如果这样做,我建议您遵循该顺序,它将更快),您可以使用凸包:
If you don't know how your points are set up (if you do I recommend you follow that order, it will be faster) you can use Convex Hull from scipy:
import matplotlib.pyplot as plt
from scipy.spatial import ConvexHull
# RANDOM DATA
x = np.random.normal(0,1,100)
y = np.random.normal(0,1,100)
xy = np.hstack((x[:,np.newaxis],y[:,np.newaxis]))
# PERFORM CONVEX HULL
hull = ConvexHull(xy)
# PLOT THE RESULTS
plt.scatter(x,y)
plt.plot(x[hull.vertices], y[hull.vertices])
plt.show()
,在上面的示例中结果是这样的:
, which in the example above results is this:
请注意,此方法将为您的点创建一个边界框.
Notice this method will create a bounding box for your points.
这篇关于在matplotlib中绘制一组给定点以形成闭合曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!