我有一个图,其中每个边都有两个属性“标记”和“电缆名称”。我的目标是通过一次显示两个属性来绘制它,一个显示在另一个之上:
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()
它仅显示一个属性“标记”,正如我从documentation中获得的 get_edge_attributes 函数一样,其属性“名称”只能是一个字符串,而不是字符串列表。 / p>
是否有可能以这种方式显示两个属性?
答案 0 :(得分:1)
您可以通过将要在绘图上显示的标签与关键点(即边缘)相关联来自定义边缘标签。
示例:
格式化标签
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