Skip to content

Commit c07c804

Browse files
committed
Example on hovering neighborhoods
1 parent 0ae3027 commit c07c804

3 files changed

Lines changed: 90 additions & 4 deletions

File tree

docs/source/sg_execution_times.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
Computation times
88
=================
9-
**00:00.046** total execution time for 88 files **from all galleries**:
9+
**00:00.100** total execution time for 89 files **from all galleries**:
1010

1111
.. container::
1212

@@ -32,8 +32,8 @@ Computation times
3232
* - Example
3333
- Time
3434
- Mem (MB)
35-
* - :ref:`sphx_glr_gallery_style_plot_edge_multicolor.py` (``../../gallery/style/plot_edge_multicolor.py``)
36-
- 00:00.046
35+
* - :ref:`sphx_glr_gallery_other_plot_hover_neighborhood.py` (``../../gallery/other/plot_hover_neighborhood.py``)
36+
- 00:00.100
3737
- 0.0
3838
* - :ref:`sphx_glr_gallery_basic_plot_3d.py` (``../../gallery/basic/plot_3d.py``)
3939
- 00:00.000
@@ -194,6 +194,9 @@ Computation times
194194
* - :ref:`sphx_glr_gallery_style_plot_edge_geometries.py` (``../../gallery/style/plot_edge_geometries.py``)
195195
- 00:00.000
196196
- 0.0
197+
* - :ref:`sphx_glr_gallery_style_plot_edge_multicolor.py` (``../../gallery/style/plot_edge_multicolor.py``)
198+
- 00:00.000
199+
- 0.0
197200
* - :ref:`sphx_glr_gallery_style_plot_edgepadding.py` (``../../gallery/style/plot_edgepadding.py``)
198201
- 00:00.000
199202
- 0.0
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
Hover neighborhoods
3+
===================
4+
5+
This example shows how to highlight a node and its neighborhood by hovering with the mouse on it.
6+
7+
.. warning::
8+
This example will run in a Python, IPython, or Jupyter session, however the
9+
interactive functionality is not visible on the HTML page. Download the code
10+
at the end of this page and run it in a local Python environment to see
11+
the results.
12+
"""
13+
14+
import matplotlib.pyplot as plt
15+
import igraph as ig
16+
import iplotx as ipx
17+
18+
g = ig.Graph.Erdos_Renyi(n=40, m=120)
19+
layout = g.layout()
20+
21+
fig, axs = plt.subplots(1, 2, gridspec_kw={"width_ratios": [1, 0.2]}, figsize=(8, 6))
22+
art = ipx.network(
23+
g,
24+
layout=layout,
25+
ax=axs[0],
26+
aspect=1,
27+
vertex_zorder=5,
28+
)[0]
29+
base_nodecolors = art.get_vertices().get_facecolors()
30+
nodecolors = base_nodecolors.copy()
31+
edgecolors = art.get_edges().get_edgecolors()
32+
edgewidths = art.get_edges().get_linewidths()
33+
34+
axs[1].set_axis_off()
35+
txt = axs[1].text(0.5, 0.5, "", ha="center", va="center", wrap=True,
36+
fontsize=40)
37+
38+
39+
def hover_callback(event):
40+
"""React to mouse hovering over vertices."""
41+
if event.inaxes == axs[0]:
42+
cont, ind = art.get_vertices().contains(event)
43+
44+
# Reset everyone's color
45+
is_base = (nodecolors == base_nodecolors).all()
46+
47+
# If mouse is over a vertex, change the color around there
48+
# and redraw
49+
if cont:
50+
i = ind["ind"][0]
51+
nodecolors[:] = [0, 0, 0, 0.3]
52+
nodecolors[g.neighbors(i)] = [1, 0, 0, 0.6]
53+
nodecolors[i] = [1, 0, 0, 1]
54+
art.get_vertices().set_facecolors(nodecolors)
55+
edgecolors[:] = [0, 0, 0, 0.3]
56+
edgecolors[g.incident(i)] = [0, 0, 0, 1]
57+
art.get_edges().set_edgecolors(edgecolors)
58+
edgewidths[:] = 1
59+
edgewidths[g.incident(i)] = 2
60+
art.get_edges().set_linewidths(edgewidths)
61+
62+
txt.set_text(str(i))
63+
# Otherwise, change back to base and redraw
64+
elif not is_base:
65+
nodecolors[:] = base_nodecolors
66+
vertex_artist.set_facecolors(nodecolors)
67+
edgecolors[:] = [0, 0, 0, 1]
68+
art.get_edges().set_edgecolors(edgecolors)
69+
edgewidths[:] = 1
70+
art.get_edges().set_linewidths(edgewidths)
71+
txt.set_text("")
72+
# If nothing changed, no need to redraw
73+
else:
74+
return
75+
76+
# Redraw if needed
77+
fig.canvas.draw_idle()
78+
79+
80+
fig.canvas.mpl_connect(
81+
"motion_notify_event",
82+
hover_callback,
83+
)

gallery/other/plot_mouse_hover.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def hover_callback(event):
4343
if event.inaxes == ax:
4444
vc = art.get_vertices()
4545
cont, ind = vc.contains(event)
46-
# If mouse is over a vertex, show the buble
46+
# If mouse is over a vertex, show the bubble
4747
if cont:
4848
i = ind["ind"][0]
4949
annot.xy = vc.get_offsets()[i]

0 commit comments

Comments
 (0)