Drawing multiple edges between two nodes with networkx(使用networkx在两个节点之间绘制多条边)
问题描述
我需要在两个节点之间绘制一个有多个边(具有不同权重)的有向图.也就是说,我有节点 A 和 B 以及长度为 2 的边 (A,B) 和长度为 3 的边 (B,A).
I need to draw a directed graph with more than one edge (with different weights) between two nodes. That is, I have nodes A and B and edges (A,B) with length=2 and (B,A) with length=3.
我已经尝试过使用 G=nx.Digraph 和 G=nx.Multidigraph.当我绘制它时,我只能看到一个边缘和一个标签.有什么办法吗?
I have tried both using G=nx.Digraph and G=nx.Multidigraph. When I draw it, I only get to view one edge and only one of the labels. Is there any way to do it?
推荐答案
对上面回复的改进是添加 connectionstyle 到 nx.draw,这样可以在图中看到两条平行线:
An improvement to the reply above is adding the connectionstyle to nx.draw, this allows to see two parallel lines in the plot:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.DiGraph() #or G = nx.MultiDiGraph()
G.add_node('A')
G.add_node('B')
G.add_edge('A', 'B', length = 2)
G.add_edge('B', 'A', length = 3)
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True, connectionstyle='arc3, rad = 0.1')
edge_labels=dict([((u,v,),d['length'])
for u,v,d in G.edges(data=True)])
plt.show()
这篇关于使用networkx在两个节点之间绘制多条边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用networkx在两个节点之间绘制多条边


- 沿轴计算直方图 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01