我有一组聚类,并提取了每个聚类的质心坐标,并将其绘制在散点图上。这是坐标:
112 59
214 90
244 182
254 167
255 112
261 139
283 152
291 134
314 138
334 49
333 34
以下是绘制散点图的代码:
import pylab as pl
import matplotlib.pyplot as plt
import numpy as np
data = np.loadtxt('centroid_coordinates.txt')
pl.scatter(data[:,0], data[:,1])
pl.xlabel('x-coordinate')
pl.ylabel('y-coordinate')
#print result
plt.title('Centroids')
pl.show()
现在,我想将质心节点连接在一起以形成图以显示其空间对映关系。有什么建议吗?
答案 0 :(得分:0)
networkx
是你的朋友:
import matplotlib.pyplot as plt
import networkx as nx
import itertools
nodes = [[112, 59],
[214, 90],
[244, 182],
[254, 167],
[255, 112],
[261, 139],
[283, 152],
[291, 134],
[314, 138],
[334, 49],
[333, 34]]
# initialise empty graph
G = nx.Graph()
# create dictionary for node positions
positions={}
for node, properties in enumerate(nodes):
positions[node] = properties
# add the nodes to the graph
G.add_nodes_from(positions.keys())
# list all possible combinations of nodes within the graph
edges = itertools.product(positions.keys(), positions.keys())
# and add them to the graph
G.add_edges_from(edges)
# draw nodes and edges, positioned at the centroids
nx.draw_networkx_nodes(G, pos=positions)
nx.draw_networkx_edges(G, pos=positions)