我和这个有同样的问题。该解决方案有效,但是,我似乎无法将节点隔开并使它们与我的数据集一起以圆形格式显示。我总共有大约 30 个用颜色编码的节点。
相同颜色的节点是重叠的,而不是以圆形格式/更同心的形式聚集在一起。
我使用了上面问题中的代码,并尝试了所有可能的半径值,但似乎无法使相同颜色的节点形成一个圆圈。
代码:
import networkx
import numpy as np
import matplotlib.pyplot as plt
nodesWithGroup = {'A':'#7a8eff', 'B': '#7a8eff', 'C': '#eb2c30', 'D':'#eb2c30', 'E': '#eb2c30', 'F':'#730a15', 'G': '#730a15'}
# Set up graph, adding nodes and edges
G = nx.Graph()
G.add_nodes_from(nodesWithGroup.keys())
# Create a dictionary mapping color to a list of nodes
nodes_by_color = {}
for k, v in nodesWithGroup.items():
if v not in nodes_by_color:
nodes_by_color[v] = [k]
else:
nodes_by_color[v].append(k)
# Create …
Run Code Online (Sandbox Code Playgroud) 我有一个加权圆形布局图。我想让边缘从节点的外部开始,但找不到这样做的方法。我尝试设置 alpha=1,但这并没有给我想要的结果。下图显示了我现在得到的
这是我现在对节点的以下代码:
for n in G.nodes():
if n in set1:
G.nodes[n]['color'] = '#7a8eff'
elif n in set2:
G.nodes[n]['color'] = '#eb2c30'
elif n in set3:
G.nodes[n]['color'] = '#7300ff'
else:
G.nodes[n]['color'] = '#730a15'
colors = [node[1]['color'] for node in G.nodes(data=True)]
nx.draw_networkx_nodes(G, pos, node_size=1000, node_color=colors)
# edges
for edge in G.edges():
source, target = edge
rad = 0.25
node_color_dict = dict(G.nodes(data='color'))
if node_color_dict[source] == node_color_dict[target]:
arrowprops=dict(lw=G.edges[(source,target)]['weight'],
arrowstyle="-",
color='blue',
connectionstyle=f"arc3,rad={rad}",
linestyle= '-',
alpha=0.45)
ax.annotate("",
xy=pos[source],
xytext=pos[target],
arrowprops=arrowprops
)
else:
arrowprops=dict(lw=G.edges[(source,target)]['weight'],
arrowstyle="-",
color='purple',
connectionstyle=f"arc3,rad={rad}", …
Run Code Online (Sandbox Code Playgroud) 我目前的networkx
图表有以下代码:
import matplotlib.pyplot as plt
import networkx as nx
g = nx.Graph()
# add edges
g.add_edge("a", "b", weight=0.6)
g.add_edge("a", "c", weight=0.2)
g.add_edge("c", "d", weight=0.1)
g.add_edge("c", "e", weight=0.7)
g.add_edge("c", "f", weight=0.9)
g.add_edge("a", "d", weight=0.3)
# group edges by attribute "weight"
elarge = [
(u, v) for (u, v, d) in g.edges(data=True)
if d["weight"] > 0.5]
esmall = [
(u, v) for (u, v, d) in g.edges(data=True)
if d["weight"] <= 0.5]
# compute the positions of nodes
pos = nx.circular_layout(g) …
Run Code Online (Sandbox Code Playgroud)