Networkx,整数节点的定位,而不是字符串
Networkx, positioning of nodes that are integers, not strings
我的目标是相对定位两个图表。
我为每个图单独保留节点集和节点标签。
n, p, seedd = 5, 0.7, 13
g1 = fast_gnp_random_graph(3, 1, seed = seedd, directed = False)
g2 = fast_gnp_random_graph(n, p, seed = seedd, directed = False)
# making sure nodes are not duplicated for drawing edges later.
dup = 0
for i in g1.nodes:
if g2.has_node(i):
dup = dup +1
g2 = relabel_nodes(g2, {i:i+dup for i in g2.nodes})
g1_labels = {i: label for i, label in enumerate(['appel', 'peahc', 'pppuy'])}
g2_labels = {i: label for i, label in zip(g2.nodes, ['ape', 'apple', 'peach', 'puppy', 'rand'])}
set_node_attributes(g1, g1_labels, 'label')
set_node_attributes(g2, g2_labels, 'label')
pos_1 = nx.spring_layout(g1)
pos_2 = nx.spring_layout(g2)
# shift g2 away from g1 for plotting.
for node in g2:
pos_2[node][0] += 4
# map nodes across g1 and g2 randomly.
g2_map = choice(g2.nodes, number_of_nodes(g1), replace = False)
new_bipartite = [[i, j] for i, j in zip(g1.nodes, g2_map)]
B = Graph()
B.add_nodes_from(g1.nodes, bipartite = 0)
B.add_nodes_from(g2.nodes, bipartite = 1)
B.add_edges_from(new_bipartite)
figure(figsize = (15, 15))
draw_networkx(g1, pos_1, node_color = '#5499c7', node_size = 400, with_labels = False)
draw_networkx(g2, pos_2, node_color = '#d35400', node_size = 400, with_labels = False)
g1_color = ['#5499c7' for node in range(0, number_of_nodes(g1))]
g2_color = ['#d35400' for node in range(0, number_of_nodes(g2))]
draw_networkx(B, pos = dict(pos_1, **pos_2), width = 2, style = 'dashed', node_color = g1_color + g2_color, node_size = 400, font_size = 28)
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_9792/3082168389.py in <module>
2 draw_networkx(g1, pos_1, node_color = '#5499c7', node_size = 400, with_labels = False)
3 draw_networkx(g2, pos_2, node_color = '#d35400', node_size = 400, with_labels = False)
----> 4 draw_networkx(B, pos = dict(pos_1, **pos_2), type = 'dashed')
TypeError: keywords must be strings
我想将 g1 和 g2 节点保留为整数以用于下游索引,但是,位置字典需要字符串作为键。
我将节点与标签分开,以确保当两个图具有相同节点时我不会出现自循环。
这是我在设置 set_node_attributes 后插入的行以获得正确的情节。
g1 = relabel_nodes(g1, g1_labels)
g2 = relabel_nodes(g2, g2_labels)
我认为加入两个dicts只是一个小问题。所有进口:
import networkx as nx
import matplotlib.pylab as pl
import numpy as np
n, p, seedd = 5, 0.7, 13
g1 = nx.fast_gnp_random_graph(3, 1, seed=seedd, directed=False)
g2 = nx.fast_gnp_random_graph(n, p, seed=seedd, directed=False)
# making sure nodes are not duplicated for drawing edges later.
dup = 0
for i in g1.nodes:
if g2.has_node(i):
dup = dup + 1
g2 = nx.relabel_nodes(g2, {i: i + dup for i in g2.nodes})
g1_labels = {i: label for i, label in enumerate(['appel', 'peahc', 'pppuy'])}
g2_labels = {i: label for i, label in zip(g2.nodes, ['ape', 'apple', 'peach', 'puppy', 'rand'])}
nx.set_node_attributes(g1, g1_labels, 'label')
nx.set_node_attributes(g2, g2_labels, 'label')
pos_1 = nx.spring_layout(g1)
pos_2 = nx.spring_layout(g2)
# shift g2 away from g1 for plotting.
for node in g2:
pos_2[node][0] += 4
# map nodes across g1 and g2 randomly.
g2_map = np.random.choice(g2.nodes, nx.number_of_nodes(g1), replace=False)
new_bipartite = [[i, j] for i, j in zip(g1.nodes, g2_map)]
B = nx.Graph()
B.add_nodes_from(g1.nodes, bipartite=0)
B.add_nodes_from(g2.nodes, bipartite=1)
B.add_edges_from(new_bipartite)
pl.figure(figsize=(15, 15))
nx.draw_networkx(g1, pos_1, node_color='#5499c7', node_size=400, with_labels=False)
nx.draw_networkx(g2, pos_2, node_color='#d35400', node_size=400, with_labels=False)
g1_color = ['#5499c7' for node in range(0, nx.number_of_nodes(g1))]
g2_color = ['#d35400' for node in range(0, nx.number_of_nodes(g2))]
nx.draw_networkx(B, pos={**pos_1, **pos_2}, width=2, style='dashed', node_color=g1_color + g2_color, node_size=400,
font_size=28)
我的目标是相对定位两个图表。
我为每个图单独保留节点集和节点标签。
n, p, seedd = 5, 0.7, 13
g1 = fast_gnp_random_graph(3, 1, seed = seedd, directed = False)
g2 = fast_gnp_random_graph(n, p, seed = seedd, directed = False)
# making sure nodes are not duplicated for drawing edges later.
dup = 0
for i in g1.nodes:
if g2.has_node(i):
dup = dup +1
g2 = relabel_nodes(g2, {i:i+dup for i in g2.nodes})
g1_labels = {i: label for i, label in enumerate(['appel', 'peahc', 'pppuy'])}
g2_labels = {i: label for i, label in zip(g2.nodes, ['ape', 'apple', 'peach', 'puppy', 'rand'])}
set_node_attributes(g1, g1_labels, 'label')
set_node_attributes(g2, g2_labels, 'label')
pos_1 = nx.spring_layout(g1)
pos_2 = nx.spring_layout(g2)
# shift g2 away from g1 for plotting.
for node in g2:
pos_2[node][0] += 4
# map nodes across g1 and g2 randomly.
g2_map = choice(g2.nodes, number_of_nodes(g1), replace = False)
new_bipartite = [[i, j] for i, j in zip(g1.nodes, g2_map)]
B = Graph()
B.add_nodes_from(g1.nodes, bipartite = 0)
B.add_nodes_from(g2.nodes, bipartite = 1)
B.add_edges_from(new_bipartite)
figure(figsize = (15, 15))
draw_networkx(g1, pos_1, node_color = '#5499c7', node_size = 400, with_labels = False)
draw_networkx(g2, pos_2, node_color = '#d35400', node_size = 400, with_labels = False)
g1_color = ['#5499c7' for node in range(0, number_of_nodes(g1))]
g2_color = ['#d35400' for node in range(0, number_of_nodes(g2))]
draw_networkx(B, pos = dict(pos_1, **pos_2), width = 2, style = 'dashed', node_color = g1_color + g2_color, node_size = 400, font_size = 28)
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_9792/3082168389.py in <module>
2 draw_networkx(g1, pos_1, node_color = '#5499c7', node_size = 400, with_labels = False)
3 draw_networkx(g2, pos_2, node_color = '#d35400', node_size = 400, with_labels = False)
----> 4 draw_networkx(B, pos = dict(pos_1, **pos_2), type = 'dashed')
TypeError: keywords must be strings
我想将 g1 和 g2 节点保留为整数以用于下游索引,但是,位置字典需要字符串作为键。
我将节点与标签分开,以确保当两个图具有相同节点时我不会出现自循环。
这是我在设置 set_node_attributes 后插入的行以获得正确的情节。
g1 = relabel_nodes(g1, g1_labels)
g2 = relabel_nodes(g2, g2_labels)
我认为加入两个dicts只是一个小问题。所有进口:
import networkx as nx
import matplotlib.pylab as pl
import numpy as np
n, p, seedd = 5, 0.7, 13
g1 = nx.fast_gnp_random_graph(3, 1, seed=seedd, directed=False)
g2 = nx.fast_gnp_random_graph(n, p, seed=seedd, directed=False)
# making sure nodes are not duplicated for drawing edges later.
dup = 0
for i in g1.nodes:
if g2.has_node(i):
dup = dup + 1
g2 = nx.relabel_nodes(g2, {i: i + dup for i in g2.nodes})
g1_labels = {i: label for i, label in enumerate(['appel', 'peahc', 'pppuy'])}
g2_labels = {i: label for i, label in zip(g2.nodes, ['ape', 'apple', 'peach', 'puppy', 'rand'])}
nx.set_node_attributes(g1, g1_labels, 'label')
nx.set_node_attributes(g2, g2_labels, 'label')
pos_1 = nx.spring_layout(g1)
pos_2 = nx.spring_layout(g2)
# shift g2 away from g1 for plotting.
for node in g2:
pos_2[node][0] += 4
# map nodes across g1 and g2 randomly.
g2_map = np.random.choice(g2.nodes, nx.number_of_nodes(g1), replace=False)
new_bipartite = [[i, j] for i, j in zip(g1.nodes, g2_map)]
B = nx.Graph()
B.add_nodes_from(g1.nodes, bipartite=0)
B.add_nodes_from(g2.nodes, bipartite=1)
B.add_edges_from(new_bipartite)
pl.figure(figsize=(15, 15))
nx.draw_networkx(g1, pos_1, node_color='#5499c7', node_size=400, with_labels=False)
nx.draw_networkx(g2, pos_2, node_color='#d35400', node_size=400, with_labels=False)
g1_color = ['#5499c7' for node in range(0, nx.number_of_nodes(g1))]
g2_color = ['#d35400' for node in range(0, nx.number_of_nodes(g2))]
nx.draw_networkx(B, pos={**pos_1, **pos_2}, width=2, style='dashed', node_color=g1_color + g2_color, node_size=400,
font_size=28)