1. 经纬度数据
xy = np.array([[29.060703, 113.348653], [22.5445697, 114.0545346],
[43.8882168, 125.3182578], [30.5951051, 114.2999353],
[26.0774954, 119.2918215], [23.0444712, 113.7465512],
[42.0151733, 121.6452868], [32.0609736, 118.7916458],
[22.5445697, 114.0545346], [39.1235635, 117.1980785],
[36.6507007, 117.1140042], [31.2252985, 121.4890497],
[20.0423626, 110.3409936], [22.5213807, 113.3656141],
[29.3595966, 115.9428552], [26.5814112, 106.7052544],
[23.1301964, 113.2592945], [41.494444, 121.615278],
[23.0444712, 113.7465512], [39.906217, 116.3912757],
[22.5213807, 113.3656141], [9.6153904, 112.970189230798],
[39.1235635, 117.1980785], [30.466238, 111.686209455868],
[45.7656666, 126.6160584], [32.5664035, 128.899626584395],
[43.8882168, 125.3182578], [23.0515551, 112.4728451],
[31.32640095, 120.990580800174], [27.9958689, 120.6915792],
[39.906217, 116.3912757], [22.5445697, 114.0545346],
[35.218039, 111.053715], [39.906217, 116.3912757],
[31.9827896, 120.8904588], [31.458932, 104.750108],
[34.7591877, 113.6524076], [27.9958689, 120.6915792],
[39.906217, 116.3912757], [39.1235635, 117.1980785],
[32.0609736, 118.7916458], [31.8129864, 119.9692045],
[31.8782976, 120.5514595], [39.1235635, 117.1980785],
[29.5585712, 106.5492822], [46.8087794, 130.3734556],
[31.2252985, 121.4890497], [39.1235635, 117.1980785],
[34.3468421, 108.9365887], [22.5445697, 114.0545346],
[31.8129864, 119.9692045], [39.906217, 116.3912757],
[33.387124, 104.9313723], [31.2252985, 121.4890497],
[25.4342199, 119.0074625], [43.8254059, 87.614569],
[39.906217, 116.3912757], [39.906217, 116.3912757],
[24.4586961, 118.0745391], [40.01363995, 119.35495441411],
[34.7591877, 113.6524076], [30.5951051, 114.2999353],
[30.65093955, 120.783165577059], [39.906217, 116.3912757],
[30.5951051, 114.2999353], [29.5585712, 106.5492822],
[23.1301964, 113.2592945], [39.1235635, 117.1980785],
[28.6581723, 121.4163876], [40.6549157, 109.8344586],
[39.906217, 116.3912757], [22.8211654, 108.3159756],
[32.0609736, 118.7916458], [23.1301964, 113.2592945],
[38.04, 114.47], [30.7031196, 111.2750181],
[34.0472604, 113.804958141593], [26.0774954, 119.2918215],
[34.3468421, 108.9365887], [39.906217, 116.3912757],
[23.0247687, 113.1146335], [27.13543065, 105.565762465037],
[32.0609736, 118.7916458], [43.6392011, 122.2497308],
[29.8622194, 121.6203873], [30.2134399, 115.0670135],
[38.21211485, 116.838327521023], [21.9745252, 113.931350084859],
[29.7275361, 115.9913134], [31.2252985, 121.4890497],
[28.1979483, 112.97133], [26.6655699, 119.5224895],
[29.75929015, 120.51582161865], [29.5585712, 106.5492822],
[36.4441808, 115.9642007]]
)
2. 坐标聚类生成子图
for k in range(2,6):
matplotlib.rcParams['font.sans-serif']=['SimHei']
if k == 2:
ax = fig.add_subplot(221)
ax.set_title('聚类图(2个簇点)')
if k == 3:
ax = fig.add_subplot(222)
ax.set_title('聚类图(3个簇点)')
if k == 4:
ax = fig.add_subplot(223)
ax.set_title('聚类图(4个簇点)')
if k == 5:
ax = fig.add_subplot(224)
ax.set_title('聚类图(5个簇点)')
clf = KMeans(n_clusters=k)
s = clf.fit(xy)
numSamples=len(xy)
centroids = clf.labels_
mark = ['or', 'ob', 'og', 'ok', '^r', '+r', 'sr', 'dr', '<r', 'pr']
for i in range(numSamples):
ax.plot(xy[i][1], xy[i][0], mark[clf.labels_[i]])
mark = ['Dr', 'Db', 'Dg', 'Dk', '^b', '+b', 'sb', 'db', '<b', 'pb']
centroids = clf.cluster_centers_
for i in range(k):
ax.plot(centroids[i][1], centroids[i][0], mark[i], markersize = 12)
plt.subplots_adjust(wspace =0.3, hspace =0.5)
plt.savefig('聚类图.png', dpi=200)
plt.show()
3. 结果展示
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190621113006599.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTgyNDQyOQ==,size_16,color_FFFFFF,t_70)