他们是否有可能在 netgraph 中执行一些鼠标悬停以突出显示节点?
Is their a possibility to perform some mouse over in netgraph for node highlighting?
基于 问题,可以生成一个用 netgraph 可视化的 networkx 图:
import matplotlib.pyplot as plt
import networkx as nx
from netgraph import Graph # pip install netgraph
node_labels = {1: 'p→q', 2: '¬q', 3: '¬ (¬p)', 4: '¬p', 5: '¬p∧ ¬ (¬p)', 6: 'p', 7: 'q', 8: 'q∧ ¬q', 9: '¬p'}
color_map = {1: 'red', 2: 'red', 3: 'red', 4: 'red', 5: 'lightblue', 6: 'lightblue', 7: 'lightblue', 8: 'lightblue', 9: 'blue'}
edge_labels = {(3, 5): '∧I', (4, 5): '∧I', (4, 6): '¬E', (5, 6): '¬E', (1, 7): '→E', (6, 7): '→E', (2, 8): '∧I', (7, 8): '∧I', (8, 9): '¬E', (3, 9): '¬E'}
highlight = {1: {1}, 2: {2}, 3: {3}, 4: {4}, 5: {3, 4}, 6: {3}, 7: {1, 3}, 8: {1, 2, 3}, 9: {1, 2}}
graph = nx.from_edgelist(edge_labels, nx.DiGraph())
Graph(graph, node_layout='dot',
node_labels=node_labels, node_label_fontdict=dict(size=21),
edge_labels=edge_labels, edge_label_fontdict=dict(size=14), edge_label_rotate=False,
node_color=color_map, node_edge_color=color_map, arrows=True
)
plt.show()
我现在正在寻找一种简单的方法来在鼠标悬停时突出显示其上的节点。所以我创建了字典突出显示。通过示例它具有以下含义:
- 如果我将鼠标悬停在索引为
7
(q
) 的节点上,他们的
应突出显示节点 1
(p→q
) 和 3
(¬ (¬p)
)
连同 7
.
在 netgraph 上,我发现了一些 interactive graphs 的教程,这些教程对节点及其后继者和前任者做了类似的事情。但是在该教程中并不清楚突出显示的具体工作原理。
如果你能再次帮助我,我将不胜感激。谢谢!
如您所述,InteractiveGraph
class 实现了某些悬停事件,这样如果您将鼠标悬停在节点上,该节点及其相邻节点以及它们之间的边缘就会突出显示;如果将鼠标悬停在一条边上,则该边及其源节点和目标节点会突出显示。但是,netgraph 不支持自定义悬停事件(目前)。
但是,所需的功能实现起来并不难。 InteractiveGraph
继承了EmphasizeOnHoverGraph
的节点和边高亮逻辑。 EmphasizeOnHoverGraph._on_motion
would need some major changes, which is where the logic for highlighting other elements in the graph is implemented at the moment. Also, EmphasizeOnHoverGraph.__init__
and InteractiveGraph.__init__
would need some minor changes to support the additional keyword argument. If you raise an issue on my github,我不会忘记下周或下周查看它。如果您想自己尝试一下,欢迎提出请求!
基于
import matplotlib.pyplot as plt
import networkx as nx
from netgraph import Graph # pip install netgraph
node_labels = {1: 'p→q', 2: '¬q', 3: '¬ (¬p)', 4: '¬p', 5: '¬p∧ ¬ (¬p)', 6: 'p', 7: 'q', 8: 'q∧ ¬q', 9: '¬p'}
color_map = {1: 'red', 2: 'red', 3: 'red', 4: 'red', 5: 'lightblue', 6: 'lightblue', 7: 'lightblue', 8: 'lightblue', 9: 'blue'}
edge_labels = {(3, 5): '∧I', (4, 5): '∧I', (4, 6): '¬E', (5, 6): '¬E', (1, 7): '→E', (6, 7): '→E', (2, 8): '∧I', (7, 8): '∧I', (8, 9): '¬E', (3, 9): '¬E'}
highlight = {1: {1}, 2: {2}, 3: {3}, 4: {4}, 5: {3, 4}, 6: {3}, 7: {1, 3}, 8: {1, 2, 3}, 9: {1, 2}}
graph = nx.from_edgelist(edge_labels, nx.DiGraph())
Graph(graph, node_layout='dot',
node_labels=node_labels, node_label_fontdict=dict(size=21),
edge_labels=edge_labels, edge_label_fontdict=dict(size=14), edge_label_rotate=False,
node_color=color_map, node_edge_color=color_map, arrows=True
)
plt.show()
我现在正在寻找一种简单的方法来在鼠标悬停时突出显示其上的节点。所以我创建了字典突出显示。通过示例它具有以下含义:
- 如果我将鼠标悬停在索引为
7
(q
) 的节点上,他们的 应突出显示节点1
(p→q
) 和3
(¬ (¬p)
) 连同7
.
在 netgraph 上,我发现了一些 interactive graphs 的教程,这些教程对节点及其后继者和前任者做了类似的事情。但是在该教程中并不清楚突出显示的具体工作原理。
如果你能再次帮助我,我将不胜感激。谢谢!
如您所述,InteractiveGraph
class 实现了某些悬停事件,这样如果您将鼠标悬停在节点上,该节点及其相邻节点以及它们之间的边缘就会突出显示;如果将鼠标悬停在一条边上,则该边及其源节点和目标节点会突出显示。但是,netgraph 不支持自定义悬停事件(目前)。
但是,所需的功能实现起来并不难。 InteractiveGraph
继承了EmphasizeOnHoverGraph
的节点和边高亮逻辑。 EmphasizeOnHoverGraph._on_motion
would need some major changes, which is where the logic for highlighting other elements in the graph is implemented at the moment. Also, EmphasizeOnHoverGraph.__init__
and InteractiveGraph.__init__
would need some minor changes to support the additional keyword argument. If you raise an issue on my github,我不会忘记下周或下周查看它。如果您想自己尝试一下,欢迎提出请求!