基于顶点位置创建 NetworkX 加权图
Create NetworkX weighted Graph based on vertices location
我以一种方式组织边信息,显示边在常规类型图形中的位置;其中规则图定义为看起来像 "checker-board" 的图。以下是数据组织方式的示例:
(7, 3) (6, 3) 1.0
(7, 3) (8, 3) 1.0
(7, 3) (8, 2) 1.41421356237
(7, 3) (6, 4) 1.41421356237
(7, 3) (7, 4) 1.0
(7, 3) (6, 2) 1.41421356237
(7, 3) (7, 2) 1.0
(7, 3) (8, 4) 1.41421356237
此处,第 1 列表示第一个点的位置(例如,第一个点向上 7 列,向下 3 行),第 2 列表示相邻点,第 3 列表示两点之间的边权重值。提供的示例显示了位置 (7,3) 处的点的所有可能的相邻路径(包括对角线)。
我用这些边创建图形的代码如下所示:
import networkx as nx
edgelist = r'C:\filepath'
edges = nx.read_weighted_edgelist(edgelist, create_using= nx.Graph(), nodetype= int)
nx.draw_networkx(edges)
我没有收到错误,但我只收到一个空输出。关于如何纠正这个的任何想法?我正在使用 Python 27。谢谢!
原则上,节点标识与其位置无关。您可以通过为每个节点创建一个虚拟身份来简化问题,并将节点位置存储在不同的数据结构中,然后在绘制图形时使用。例如,您可以执行以下操作:
import numpy as np
import matplotlib.pyplot as plt
import re
import networkx as nx
def load(fpath):
# adapated from:
data = []
pattern='\((\d+, \d+)\)'
with open(fpath, 'r') as f:
for line in f:
matches = re.findall(pattern, line)
source, target = [tuple(map(lambda x:int(x),item.split(','))) for item in matches]
weight = float(line.split(' ')[-1])
data.append((source, target, weight))
return data
data = load('test_grid.txt')
# print data
# determine unique nodes
sources = [source for source, _, _ in data]
targets = [target for _, target, _ in data]
coordinates = set(sources + targets)
# create mappings from node -> coordinate and vice versa
pos = {ii : xy for ii, xy in enumerate(coordinates)}
inverse = {xy : ii for ii, xy in enumerate(coordinates)}
# create a more conventional edge list
edges = []
for (source_coord, target_coord, weight) in data:
edge = (inverse[source_coord], inverse[target_coord], weight)
edges.append(edge)
# create graph and plot
g = nx.Graph()
g.add_weighted_edges_from(edges)
nx.draw(g, pos)
此脚本假定您的图表存储在 test_grid.txt
的文本文件中。根据需要更改路径。
我以一种方式组织边信息,显示边在常规类型图形中的位置;其中规则图定义为看起来像 "checker-board" 的图。以下是数据组织方式的示例:
(7, 3) (6, 3) 1.0
(7, 3) (8, 3) 1.0
(7, 3) (8, 2) 1.41421356237
(7, 3) (6, 4) 1.41421356237
(7, 3) (7, 4) 1.0
(7, 3) (6, 2) 1.41421356237
(7, 3) (7, 2) 1.0
(7, 3) (8, 4) 1.41421356237
此处,第 1 列表示第一个点的位置(例如,第一个点向上 7 列,向下 3 行),第 2 列表示相邻点,第 3 列表示两点之间的边权重值。提供的示例显示了位置 (7,3) 处的点的所有可能的相邻路径(包括对角线)。
我用这些边创建图形的代码如下所示:
import networkx as nx
edgelist = r'C:\filepath'
edges = nx.read_weighted_edgelist(edgelist, create_using= nx.Graph(), nodetype= int)
nx.draw_networkx(edges)
我没有收到错误,但我只收到一个空输出。关于如何纠正这个的任何想法?我正在使用 Python 27。谢谢!
原则上,节点标识与其位置无关。您可以通过为每个节点创建一个虚拟身份来简化问题,并将节点位置存储在不同的数据结构中,然后在绘制图形时使用。例如,您可以执行以下操作:
import numpy as np
import matplotlib.pyplot as plt
import re
import networkx as nx
def load(fpath):
# adapated from:
data = []
pattern='\((\d+, \d+)\)'
with open(fpath, 'r') as f:
for line in f:
matches = re.findall(pattern, line)
source, target = [tuple(map(lambda x:int(x),item.split(','))) for item in matches]
weight = float(line.split(' ')[-1])
data.append((source, target, weight))
return data
data = load('test_grid.txt')
# print data
# determine unique nodes
sources = [source for source, _, _ in data]
targets = [target for _, target, _ in data]
coordinates = set(sources + targets)
# create mappings from node -> coordinate and vice versa
pos = {ii : xy for ii, xy in enumerate(coordinates)}
inverse = {xy : ii for ii, xy in enumerate(coordinates)}
# create a more conventional edge list
edges = []
for (source_coord, target_coord, weight) in data:
edge = (inverse[source_coord], inverse[target_coord], weight)
edges.append(edge)
# create graph and plot
g = nx.Graph()
g.add_weighted_edges_from(edges)
nx.draw(g, pos)
此脚本假定您的图表存储在 test_grid.txt
的文本文件中。根据需要更改路径。