【发布时间】:2017-10-01 17:27:59
【问题描述】:
我正在做一个项目来使用 python 计算州/国家的质心。
到目前为止我做了什么:
-
绘制状态轮廓并通过 ImageJ 运行它以创建边界 x、y 坐标的 csv。这给了我一个 .csv 文件,其中包含如下数据:
556,243
557,243
557,250
556,250
556,252
555,252
555,253
554,253
等等等等
大约 2500 个数据点。
将此列表导入 Python 脚本。
计算 x 和 y 坐标数组的平均值。这个点就是质心。 (Idea similar to this)
使用 matplotlib 绘制点和质心。
这是我的代码:
#####################################################
# Imports #
#####################################################
import csv
import matplotlib.pyplot as plt
import numpy as np
import pylab
#####################################################
# Setup #
#####################################################
#Set empty list for coordinates
x,y =[],[]
#Importing csv data
with open("russiadata.csv", "r") as russiadataFile:
russiadataReader = csv.reader(russiadataFile)
#Create list of points
russiadatalist = []
#Import data
for row in russiadataReader:
#While the rows have data, AKA length not equal to zero.
if len(row) != 0:
#Append data to arrays created above
x.append(float(row[0]))
y.append(float(row[1]))
#Close file as importing is done
russiadataFile.closejust flipped around the
#####################################################
# Data Analysis #
#####################################################
#Convert list to array for computations
x=np.array(x)
y=np.array(y)
#Calculate number of data points
x_len=len(x)just flipped around the
y_len=len(y)
#Set sum of points equal to x_sum and y_sum
x_sum=np.sum(x)
y_sum=np.sum(y)
#Calculate centroid of points
x_centroid=x_sum/x_len
y_centroid=y_sum/y_len
#####################################################
# Plotting #
#####################################################
#Plot all points in data
plt.xkcd()
plt.plot(x,y, "-.")
#Plot centroid and label it
plt.plot(x_centroid,y_centroid,'^')
plt.ymax=max(x)
#Add axis labels
plt.xlabel("X")
plt.ylabel("Y")
plt.title("russia")
#Show the plot
plt.show()
我遇到的问题是该州的某些方面比其他方面有更多的点,因此质心被加权到具有更多点的区域。这不是我想要的。我试图找到具有 x,y 坐标顶点的多边形的质心。
这就是我的情节:
如您所见,质心更偏向密度更高的点部分。 (作为旁注,是的,那是俄罗斯。我遇到了情节倒退和拉伸/压扁的问题。)
也就是说,有没有更准确的方法来获取质心?
提前感谢您的帮助。
【问题讨论】:
-
听起来你想计算凸包的质心。我认为 scipy 可以为您提供凸包。然后匀称可以得到你的质心。
-
我认为甚至不需要凸包,形状应该像 Polygon(
).centroid 一样简单
标签: python python-2.7 centroid