在边缘中间有一个用箭头画边的函数吗?

2024-09-29 19:29:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想画一条中间有箭头的边。 我将networkx与Python3.6和mathblotlib一起使用来显示该图

我尝试了很多方法,但我刚刚得到了这个结果

predecesseur={'1': nan, '2': nan, '3': '1', '4': '1,2', '5': '3,4'}
tachdu={'1': '2', '2': '4', '3': '4', '4': '5', '5': '6'}
G=nx.DiGraph()
G.add_node('Début',type ='0')
G.add_node('Fin',type ='0')
for key in predecesseur.keys():
    G.add_node(key,type =tachdu[key])
    ch=predecesseur[key]
    if type(ch)!=type(0.0):
        for i in range(len(ch)):
            if (ch[i]!=',' and ch[i]!=';'):
                G.add_edge(ch[i],key)
    else:
        G.add_edge('Début',key)

for nod in G:
    if list(G.successors(nod))==[]:
        G.add_edge(nod,'Fin')

pos_nodes = nx.spring_layout(G)
nx.draw(G, pos_nodes, with_labels=True)
color_map = ['green']
nx.draw_networkx_nodes(G, pos_nodes, node_size=500, nodelist=['Fin'],node_color=color_map)
nx.draw_networkx_nodes(G, pos_nodes, node_size=1400, nodelist=['Début'],node_color=color_map)
node_attrs = nx.get_node_attributes(G, 'type')
pos_attrs = {}
for node, coords in pos_nodes.items():
    pos_attrs[node] = (coords[0], coords[1] + 0.08)
custom_node_attrs = {}
for node, attr in node_attrs.items():
    custom_node_attrs[node] = "{" + attr + "}"
nx.draw_networkx_labels(G, pos_attrs, labels=custom_node_attrs)
plt.show()

Tags: keyinposnetworkxaddnodefortype
1条回答
网友
1楼 · 发布于 2024-09-29 19:29:07

Networkx使用Matplotlib绘制图形。对于边,它使用ArrowStyle。以下是可能的边类型:

enter image description here

<不,NETWorkX不能在边缘中间画箭头。您可以尝试将其转换为点格式(或任何其他格式),并尝试使用其他工具将其可视化

相关问题 更多 >

    热门问题