【问题标题】:Networkx and Matplotlib: how to access node attributes and show them as annotationNetworkx 和 Matplotlib:如何访问节点属性并将它们显示为注释
【发布时间】:2022-01-17 06:29:13
【问题描述】:

我试图让悬停在这个 networkx matplotlib 图表上工作,但我遇到了一个关键错误问题。我想创建一个键名作为字符串的节点并将其作为标签。但是,它看起来像在xy=pos[node] 部分被称为中断的悬停功能。以下是其中一些内容的一些输出:

  • 此输出中的 obj 是我打印节点键。
  • ind 是我悬停在节点上。
  • 节点是我试图找出节点键与悬停所寻找的内容之间的联系。
pos: {'A': array([-0.88308537, -0.21952651]), 'B': array([ 0.70105714, -0.78047349]), 'C': array([0.18202824, 1.        ])}

obj: A
obj: B
obj: C
ind: {'ind': array([], dtype=int32)}
ind: {'ind': array([2], dtype=int32)}
node: 2
ind: {'ind': array([2], dtype=int32)}
node: 2
ind: {'ind': array([2], dtype=int32)}

代码如下:

import networkx as nx
import matplotlib.pyplot as plt
from networkx.readwrite import edgelist
import numpy as np

G = nx.DiGraph()

G.add_node("A", attr1=20)
G.add_node("B", attr1=25)
G.add_node("C", attr1=30)

fig, ax = plt.subplots()
pos = nx.spring_layout(G)
print('pos:', pos)

nodes = nx.draw_networkx_nodes(G, pos, node_size=500)

for obj in G.nodes:
    print('obj:', obj)

nx.draw_networkx_edges(G, pos, edgelist=G.edges(), edge_color='black')

annot = ax.annotate("", xy=(0,0), xytext=(20,20),textcoords="offset points",
                    bbox=dict(boxstyle="round", fc="w"),
                    arrowprops=dict(arrowstyle="->"))

annot.set_visible(False)

def update_annot(ind):
    node = ind["ind"][0]
    print('node:',node)
    xy = pos[node]
    print('here:', xy)
    annot.xy = xy
    node_attr = {'node': node}
    node_attr.update(G.nodes[node])
    text = '\n'.join(f'{k}: {v}' for k, v in node_attr.items())
    annot.set_text(text)

def hover(event):
    vis = annot.get_visible()
    if event.inaxes == ax:
        cont, ind = nodes.contains(event)
        print('ind:',ind)
        if cont:
            update_annot(ind)
            annot.set_visible(True)
            fig.canvas.draw_idle()
        else:
            if vis:
                annot.set_visible(False)
                fig.canvas.draw_idle()

fig.canvas.mpl_connect("motion_notify_event", hover)
plt.show()

错误:

xy = pos[node]
KeyError: 2

看起来它正在尝试查找带有数字而不是字母的键。这就是为什么我得到一个关键错误。但我不确定为什么我不能让它接受一个字符串。

【问题讨论】:

    标签: python matplotlib hover networkx


    【解决方案1】:

    node_index = ind["ind"][0] 是节点列表中的整数索引。 Networkx 使用节点名称将其节点存储在字典中。 G.nodes[2] 给出错误,因为 G.nodes 是一个按节点名称索引的字典。

    您可以将G.nodes 转换为列表,并在node_indexnode_name = list(G.nodes)[node_index] 处找到名称。使用该名称,您可以找到属性:node_attr = G.nodes[node_name]

    要使用悬停注释,我建议使用 mplcursors 库,它隐藏了许多内部簿记。

    这是更新后的示例:

    import matplotlib.pyplot as plt
    import networkx as nx
    import mplcursors
    
    G = nx.DiGraph()
    G.add_node("A", attr1=20)
    G.add_node("B", attr1=25)
    G.add_node("C", attr1=30)
    
    fig, ax = plt.subplots()
    pos = nx.spring_layout(G)
    nodes = nx.draw_networkx_nodes(G, pos, node_size=500, node_color='dodgerblue')
    
    nx.draw_networkx_labels(G, pos, font_color='yellow')
    nx.draw_networkx_edges(G, pos, edgelist=G.edges(), edge_color='black')
    
    def update_annot(sel):
        node_index = sel.target.index
        node_name = list(G.nodes)[node_index]
        node_attr = G.nodes[node_name]
        text = node_name + ' ' + '\n'.join(f'{k}: {v}' for k, v in node_attr.items())
        sel.annotation.set_text(text)
    
    cursor = mplcursors.cursor(nodes, hover=True)
    cursor.connect('add', update_annot)
    
    plt.show()
    

    【讨论】:

    • 好的,谢谢!
    猜你喜欢
    • 2012-03-27
    • 1970-01-01
    • 2012-11-21
    • 2013-12-21
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 2019-02-21
    相关资源
    最近更新 更多