|
| 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 | +) |
0 commit comments