【发布时间】: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