如何使用Python获得一组点的中心

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4355894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:24:00  来源:igfitidea点击:

How to get center of set of points using Python

pythonmatplotlibtriangulation

提问by Dominik Szopa

I would like to get the center point(x,y) of a figure created by a set of points.

我想获得由一组点创建的图形的中心点(x,y)。

How do I do this?

我该怎么做呢?

采纳答案by Colin

If you mean centroid, you just get the average of all the points.

如果你的意思是质心,你只是得到所有点的平均值。

x = [p[0] for p in points]
y = [p[1] for p in points]
centroid = (sum(x) / len(points), sum(y) / len(points))

回答by Kabie

I assume that a point is a tuple like (x,y).

我假设一个点是一个像 (x,y) 这样的元组。

x,y=zip(*points)
center=(max(x)+min(x))/2., (max(y)+min(y))/2.

回答by meduz

If the set of points is a numpy array positionsof sizes N x 2, then the centroid is simply given by:

如果点集是positions大小为 N x 2的 numpy 数组,则质心简单地由下式给出:

centroid = positions.mean(axis=0)

It will directly give you the 2 coordinates a a numpy array.

它将直接为您提供 2 个坐标 aa numpy 数组。

In general, numpy arrays can be used for all these measures in a vectorized way, which is compact and veryquick compared to forloops.

通常,numpy 数组可以以矢量化方式用于所有这些度量,与循环相比,这种方式紧凑且非常快速for