nx.draw() 将边绘制为双向,即使它们是单向的
nx.draw() plots edges as bi-directional even tho they are uni-directional
因此考虑一个每条边都是双向的格子。现在我有一些代码可以删除一些边,以减少双向边的百分比并增加单向边的百分比:
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
from random import shuffle
G = nx.grid_2d_graph(20,20, periodic=True)
G = nx.relabel.convert_node_labels_to_integers(G)
G2 = nx.DiGraph(G)
nx.set_edge_attributes(G2, values = 1, name = 'weight')
edge_list = list(G2.edges())
shuffle(edge_list)
remove_list = []
seen = []
for (u,v) in edge_list:
seen.append((u,v))
if (v,u) in edge_list:
if (v,u) not in seen:
sample = rd.sample([(u,v), (v,u)],1)[0]
if len(remove_list)< 0.8*(len(edge_list)/2):
remove_list.append(sample)
else:
break
G2.remove_edges_from(remove_list)
现在我将单向边保存在列表中:
H = [(u,v) for (u,v) in G2.edges if (v,u) not in G2.edges]
H[:10]
[(0, 20),
(0, 1),
(0, 380),
(1, 2),
(2, 22),
(2, 382),
(3, 4),
(5, 4),
(5, 25),
(5, 6)]
我现在要做的是绘制这个格子:
edge_color = ['red' if (u,v) in H else 'black' for (u,v) in G2.edges] ## uni-directional edges will be red and bi-directional ones will be black
pos = {(x,y):(y,-x) for x,y in product(range(size), range(size))}
pos1 = {i: value for i, value in zip(range(size**2), pos.values())}## assign the nodes to their positions
plt.figure(figsize=(40, 40))
nx.draw(G2,node_size=3000,node_color='lightgreen', with_labels=True, pos=pos1, width=5, edge_color = edge_color)
这个问题是我在图中有双向的红色边缘,这是错误的
让我们将 H[:10]
的输出与绘图进行比较:
如您所见,边缘 (0,1)
绘制正确,但有两个箭头表明它是双向的,即使它不是。另一个例子是边 (1,21)
,它是双向的,被涂成红色。有人可以帮我解决这个问题吗?
一个问题是您使用 G2.edges
作为列表,但实际上不是。所以说 (u,v) in G2.edges
不会检查项目成员资格,从而使您的 H 列表出错。
所以你可以这样做:
H = [(u,v) for (u,v) in G2.edges if (v,u) not in list(G2.edges)]
我不确定这是否完全解决了您的问题,但至少解决了那个问题。
G = nx.grid_2d_graph(20,20, periodic=True)
periodic (bool or iterable) – If periodic is True, both dimensions are periodic. If False, none are periodic. If periodic is iterable, it should yield 2 bool values indicating whether the 1st and 2nd axes, respectively, are periodic.
我不知道周期性网格点阵是什么,所以我做了一点研究(Google),发现了这个:
A periodic grid graph is an infinite graph obtained by concatenating horizontally an infinite number of a (finite) grid graph.
结论
这对我来说仍然很神秘,但您可以在获得的结果中看到,边缘似乎以某种方式重叠(就像绘制了不止一张图...)。
因此,由于这种“周期性”特征,您似乎获得了一些意想不到的结果。
删除 periodic=True
并将此值恢复为默认值 False
,解决了问题:
G = nx.grid_2d_graph(20,20)
因此考虑一个每条边都是双向的格子。现在我有一些代码可以删除一些边,以减少双向边的百分比并增加单向边的百分比:
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
from random import shuffle
G = nx.grid_2d_graph(20,20, periodic=True)
G = nx.relabel.convert_node_labels_to_integers(G)
G2 = nx.DiGraph(G)
nx.set_edge_attributes(G2, values = 1, name = 'weight')
edge_list = list(G2.edges())
shuffle(edge_list)
remove_list = []
seen = []
for (u,v) in edge_list:
seen.append((u,v))
if (v,u) in edge_list:
if (v,u) not in seen:
sample = rd.sample([(u,v), (v,u)],1)[0]
if len(remove_list)< 0.8*(len(edge_list)/2):
remove_list.append(sample)
else:
break
G2.remove_edges_from(remove_list)
现在我将单向边保存在列表中:
H = [(u,v) for (u,v) in G2.edges if (v,u) not in G2.edges]
H[:10]
[(0, 20),
(0, 1),
(0, 380),
(1, 2),
(2, 22),
(2, 382),
(3, 4),
(5, 4),
(5, 25),
(5, 6)]
我现在要做的是绘制这个格子:
edge_color = ['red' if (u,v) in H else 'black' for (u,v) in G2.edges] ## uni-directional edges will be red and bi-directional ones will be black
pos = {(x,y):(y,-x) for x,y in product(range(size), range(size))}
pos1 = {i: value for i, value in zip(range(size**2), pos.values())}## assign the nodes to their positions
plt.figure(figsize=(40, 40))
nx.draw(G2,node_size=3000,node_color='lightgreen', with_labels=True, pos=pos1, width=5, edge_color = edge_color)
这个问题是我在图中有双向的红色边缘,这是错误的
让我们将 H[:10]
的输出与绘图进行比较:
如您所见,边缘 (0,1)
绘制正确,但有两个箭头表明它是双向的,即使它不是。另一个例子是边 (1,21)
,它是双向的,被涂成红色。有人可以帮我解决这个问题吗?
一个问题是您使用 G2.edges
作为列表,但实际上不是。所以说 (u,v) in G2.edges
不会检查项目成员资格,从而使您的 H 列表出错。
所以你可以这样做:
H = [(u,v) for (u,v) in G2.edges if (v,u) not in list(G2.edges)]
我不确定这是否完全解决了您的问题,但至少解决了那个问题。
G = nx.grid_2d_graph(20,20, periodic=True)
periodic (bool or iterable) – If periodic is True, both dimensions are periodic. If False, none are periodic. If periodic is iterable, it should yield 2 bool values indicating whether the 1st and 2nd axes, respectively, are periodic.
我不知道周期性网格点阵是什么,所以我做了一点研究(Google),发现了这个:
A periodic grid graph is an infinite graph obtained by concatenating horizontally an infinite number of a (finite) grid graph.
结论
这对我来说仍然很神秘,但您可以在获得的结果中看到,边缘似乎以某种方式重叠(就像绘制了不止一张图...)。
因此,由于这种“周期性”特征,您似乎获得了一些意想不到的结果。
删除 periodic=True
并将此值恢复为默认值 False
,解决了问题:
G = nx.grid_2d_graph(20,20)