作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个加权圆形布局图。
我想让边缘从节点的外部开始,但找不到这样做的方法。我尝试设置 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}",
linestyle= '-',
alpha=0.45)
ax.annotate("",
xy=pos[source],
xytext=pos[target],
arrowprops=arrowprops
)
# labels
nx.draw_networkx_labels(G, pos, font_size=11, font_family="monospace", font_color='white', font_weight='bold', alpha=1.0)
最佳答案
一个完整的例子,它产生了想要的结果:
import networkx as nx
import matplotlib.pylab as pl
G = nx.karate_club_graph()
pos = nx.kamada_kawai_layout(G)
set1 = set(node for node in G if G.nodes[node]["club"] == "Mr. Hi")
set2 = set(node for node in G if G.nodes[node]["club"] != "Mr. Hi")
for n in G.nodes():
if n in set1:
G.nodes[n]['color'] = '#7a8eff'
elif n in set2:
G.nodes[n]['color'] = '#eb2c30'
for u, v in G.edges():
G.edges[(u,v)]["weight"] = 1
colors = [node[1]['color'] for node in G.nodes(data=True)]
nodes_draw = nx.draw_networkx_nodes(G, pos, node_size=1000, node_color=colors)
ax = pl.gca()
# draw in the order, edges, nodes, node labels
zorder_edges = 3
zorder_nodes = 4
zorder_node_labels = 5
# 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,
zorder=zorder_edges,
)
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}",
linestyle= '-',
#alpha=0.45,
zorder=zorder_edges,
)
ax.annotate("",
xy=pos[source],
xytext=pos[target],
arrowprops=arrowprops
)
# labels
node_labels_dict = nx.draw_networkx_labels(G, pos, font_size=11, font_family="monospace", font_color='white', font_weight='bold',
#alpha=1.0
)
nodes_draw.set_zorder(zorder_nodes)
for node_labels_draw in node_labels_dict.values():
node_labels_draw.set_zorder(zorder_node_labels)
pl.axis("off")
# do don't cut off nodes
ax.set_xlim([1.1*x for x in ax.get_xlim()])
ax.set_ylim([1.1*y for y in ax.get_ylim()])
pl.show()
结果:
zorder
创建的
matplotlib
对象:
import networkx as nx
import matplotlib.pylab as pl
# an example graph with string (names) as nodes
g = nx.karate_club_graph()
pos = nx.kamada_kawai_layout(g)
e = nx.draw_networkx_edges(g, pos=pos, )
n = nx.draw_networkx_nodes(g, pos=pos, )
e.set_zorder(5)
n.set_zorder(10)
pl.show()
如果您使用一些高级边缘绘制,请添加
zorder
all possible parameters 的箭头 Prop 参数 (
annotate ) 的参数,例如
arrowprops=dict(lw=G.edges[(source,target)]['weight'],
arrowstyle="-",
color='blue',
connectionstyle=f"arc3,rad={rad}",
linestyle= '-',
alpha=0.45,
zorder=0)
我已包含
this answer避免在边界处切割节点。
关于python - 使边从 Networkx 中的节点外部开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64803196/
我是一名优秀的程序员,十分优秀!