pythongraphnetworkxnetgraph

Is their a possibility to perform some mouse over in netgraph for node highlighting?


Based on this question it is possible to generate a networkx graph visualized with netgraph in this way:

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()

enter image description here

I am now searching an easy way to highlight nodes on it on mouse over. So I created the dictionary highlight. By example it has the following sense:

On netgraph I found some tutorials with interactive graphs which does similar things with the node and his successor and predecessor. But in that tutorial its not clear how exactly the highlighting works.

I would appreciate if you could help me again. Thank you!


Solution

  • As you noted, the InteractiveGraph class implements certain hover events such that if you hover with the mouse over a node, the node and its neighbours as well as the edges between them are highlighted; if you hover over an edge, the edge and its source and target nodes are highlighted. However, custom hover events aren't supported in netgraph (yet).

    However, the desired feature would not be too hard to implement. InteractiveGraph inherits the node and edge highlighting logic from 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, I won't forget to look into it next week or the week after. If you want to take a stab at it yourself, pull requests are always welcome!