在python networkx图中为节点添加工具提示 [英] adding tooltip for nodes in python networkx graph

查看:74
本文介绍了在python networkx图中为节点添加工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 networkx.DiGraph 创建了一个有向图,然后使用 networkx.draw_spring(graph)对其进行了绘制,因此该图的所有节点都有一些详细信息存储在词典列表.

如何在每个节点上添加类似工具提示"的内容以查看鼠标悬停时的这些详细信息?如果可能的话,如何使此工具提示"始终对所有节点可见,而不仅仅是悬停?

解决方案

始终可见

要标记所有节点,只需使用 annotate .像这样

 将matplotlib.pyplot导入为plt将networkx导入为nxG = nx.path_graph(5)attrs = {0:{'attr1':20,'attr2':'nothing'},1:{'attr2':3},2:{'attr1':42},3:{'attr3':'hello'},4:{'attr1':54,'attr3':'33'}}nx.set_node_attributes(G,attrs)nx.draw(G)对于G.nodes中的节点:xy = pos [node]annot.xy = xynode_attr = G.nodes [node]text ='\ n'.join(f'{k}:{v}'表示G.nodes [node] .items()中的k,v文字= f'node {node} \ n'+文字ax.annotate(text,xy = xy) 


悬停时

这是在悬停时获取工具提示的有效示例.这是基于使用标准matplotlib图表

I created a directed graph using networkx.DiGraph then plotted it using networkx.draw_spring(graph), so all the nodes of the graph have some details stored in a list of dictionaries.

How to add something like a "tooltip" to view these details on mouse hover on each node? If this is possible, how to make this "tooltip" always visible for all nodes, not just by hovering?

解决方案

Always visible

To label all the nodes, you just need to use annotate. Something like this

import matplotlib.pyplot as plt
import networkx as nx

G = nx.path_graph(5)
attrs = {0: {'attr1': 20, 'attr2': 'nothing'}, 1: {'attr2': 3}, 2: {'attr1': 42}, 3: {'attr3': 'hello'}, 4: {'attr1': 54, 'attr3': '33'}}
nx.set_node_attributes(G, attrs)
nx.draw(G)

for node in G.nodes:
    xy = pos[node]
    annot.xy = xy
    node_attr = G.nodes[node]
    text = '\n'.join(f'{k}: {v}' for k, v in G.nodes[node].items())
    text = f'node {node}\n' + text
    ax.annotate(text, xy=xy)


On hover

Here's a working example of getting tooltip on hover. This is based off tooltips using standard matplotlib plots here. I used draw_networkx_nodes to get the objects used for hovering and displaying tooltips instead of using draw_spring. But you can manually define the position with spring_layout.

import matplotlib.pyplot as plt
import networkx as nx

G = nx.path_graph(5)
attrs = {0: {'attr1': 20, 'attr2': 'nothing'}, 1: {'attr2': 3}, 2: {'attr1': 42}, 3: {'attr3': 'hello'}, 4: {'attr1': 54, 'attr3': '33'}}
nx.set_node_attributes(G, attrs)

fig, ax = plt.subplots()
pos = nx.spring_layout(G)
nodes = nx.draw_networkx_nodes(G, pos=pos, ax=ax)
nx.draw_networkx_edges(G, pos=pos, ax=ax)

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]
    xy = pos[node]
    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)
        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()

这篇关于在python networkx图中为节点添加工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆