[英]How to plot networkx graph with several edge attributes?
我有一個圖,其中每條邊都有兩個屬性“mark”和“cable_name”。 我的目標是繪制它同時顯示兩個屬性,一個在另一個之上:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edge(1, 2, mark='200', cable_name='К300')
pos = nx.spring_layout(G, scale=2)
edge_labels = nx.get_edge_attributes(G, 'mark')
nx.draw_networkx_edge_labels(G, pos, edge_labels)
nx.draw(G, with_labels = True, nodecolor='r', edge_color='b')
plt.show()
它只顯示一個屬性“標記”,正如我從get_edge_attributes函數的文檔中得到的那樣,它的屬性“名稱”只能是一個字符串,而不是字符串列表。
是否有適當的可能性以這樣的方式顯示這兩個屬性?
您可以通過將要顯示在繪圖上的標簽與鍵(即邊)相關聯來自定義邊標簽。
例子:
格式化標簽
edge_labels = {} # this assumes all the edges have the same labels 'marks' and 'cable_name' for u, v, data in G.edges(data=True): edge_labels[u, v] = f"{data['mark']}\\n{data['cable_name']}"
添加帶有屬性的整個字典作為邊緣標簽
edge_labels = {} for u, v, data in G.edges(data=True): edge_labels[u, v] = data
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.