Python 在节点上绘制图形

Python 在节点上绘制图形,python,Python,我有一组簇,提取了每个簇的质心坐标,并将其绘制在散点图上。以下是坐标: 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')

我有一组簇,提取了每个簇的质心坐标,并将其绘制在散点图上。以下是坐标:

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()
…其结果如下:


现在我想把质心节点连接在一起,形成一个图表,显示它的空间连接性关系。有什么建议吗

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)

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)

您可以使用Python包构建图形并显示它们。谢谢,我现在就看一看。您可以使用Python包构建图形并显示它们。谢谢,我现在就看一看。只是一个简单的问题。有没有办法不列出所有的组合?仅仅是一个简单的连通图就可以了,因为有些图有太多的节点,而图变得太拥挤了。干杯查看的文档。您可以传递一个附加参数,
edgelist
,该参数允许您指定要显示的参数。只需一个简单的问题。有没有办法不列出所有的组合?仅仅是一个简单的连通图就可以了,因为有些图有太多的节点,而图变得太拥挤了。干杯查看的文档。您可以传递一个附加参数,
edgelist
,该参数允许您指定要显示的参数。