Interactive Network Visualization

Common Use Cases for interactive network visualizations

  • social networks
  • knowledge graphs
  • dependency graphs
  • recommender systems
  • biological interaction networks

Common libraries comparison

Library Best for Scale Ease
PyVis quick interactive graphs medium very easy
Plotly dashboards & polished visuals medium medium
Bokeh custom web apps medium-large harder
Graphistry huge graphs very large medium
Netwulf research exploration small-medium very easy

PyVis

Vis.js is a lightweight browser library for highly interactive network visualizations that powers many Python tools such as PyVis.

Install: conda install -c conda-forge pyvis

Overview

  • PyVis is one of the easiest ways to create interactive graph visualizations directly in Python.

Key features

  • Built on Vis.js physics engine

  • Pan / zoom / drag nodes

  • Hover tooltips and clickable nodes

  • Works directly with NetworkX

  • Generates interactive HTML graphs

What is Vis.js?

What it is

A browser-based JavaScript visualization library for building interactive network graphs and timelines. Widely used as the rendering engine behind tools like PyVis.

Core Capabilities

Interactive graph exploration

  • Drag nodes
  • Pan / zoom
  • hover tooltips
  • selectable nodes and edges

Physics-based layouts

  • force-directed graph simulation
  • automatic node positioning
  • adjustable repulsion / attraction parameters

Dynamic graph updates

  • add/remove nodes in real time
  • animate transitions
  • update graph without full redraw

Network Visualization Model

Graph defined by two main objects:

nodes = [{id:1,label:"A"}, {id:2,label:"B"}]

edges = [{from:1,to:2}]

Rendered with

new vis.Network(container, {nodes, edges}, options)

PyVis Key Features

Highly interactive

  • node dragging
  • hover events
  • clickable links

Layout options

  • physics layout (force-directed)
  • hierarchical layout
  • fixed coordinates

Styling

  • node color, shape, size
  • edge arrows and weights
  • groups / clustering

Large graph support

  • handles thousands of nodes smoothly

Import and Setup

In PyVis, the core object you use to build, configure, and display interactive graphs is the Network object.

You create a Network to add nodes and edges, customize visualization options, and generate/export an interactive HTML file.

from pyvis.network import Network

Optional imports when building graphs:

import networkx as nx
import pandas as pd

Adding Nodes manually

net = Network(height="600px", width="100%", bgcolor="#222222", font_color="white")
net.add_node(1, label="A")
net.add_node(2, label="B")
net.add_edge(1, 2)
net.show("example.html")

Python Ecosystem

NetworkX graph
      ↓
PyVis conversion
      ↓
Vis.js interactive HTML visualization
  • Advantages
    • fast WebGL-style rendering
    • extremely interactive
    • simple graph data model
    • works directly in browsers
  • Limitations
    • primarily frontend JavaScript
    • development of the original library slowed (community forks exist)

Common PyVis functions

  • Initialize network
    • Create visualization canvas and global styling
  • Add nodes
    • Define graph entities with labels, colors, sizes, groups, and hover text
  • Add edges
    • Define relationships between nodes with weights, labels, and styles
  • Import graphs
    • Convert existing NetworkX graphs directly into PyVis visualizations
  • Configure layout physics
    • Force-directed layout controlling repulsion, gravity, and spring forces
  • Customize appearance
    • Control node size, color, shape, edge thickness, labels, and clustering
  • Enable interactive controls
    • Optional UI panel for adjusting physics, layout, and styling in the browser
  • Adjust interaction behavior
    • Hover tooltips, node dragging, zooming, and panning
  • Export visualization
    • Generate a self-contained interactive HTML network graph

Demo: PyVis

Code
import networkx as nx
from pyvis.network import Network

G = nx.erdos_renyi_graph(50, 0.05)

# To fix display issues in Jupyter on Chrome/Safari, use cdn_resources='in_line'
net = Network(notebook=True, cdn_resources='in_line')
net.from_nx(G)
net.show_buttons(filter_=['physics', "nodes", "edges"])

net.show("graph.html")

# load as iframe in jupyter notebook
from IPython.display import IFrame
IFrame("graph.html", width=800, height=600)
graph.html

NetworkX + Plotly

Steps for Interactive Network Visualization

Create an interactive network visualization with hover, zoom, and pan using Plotly while using NetworkX for graph structure and layouts.


Graph data
↓
NetworkX graph
↓
Compute layout positions
↓
Extract edge coordinates
↓
Extract node coordinates
↓
Create Plotly traces
↓
Build figure
↓
Interactive visualization

Examples: https://plotly.com/python/network-graphs/

1. Build or Load the Graph

Construct the network structure.


import networkx as nx

G = nx.karate_club_graph()

Graph data may come from:

  • edge lists
  • adjacency matrices
  • dataframes
  • APIs or databases

2. Compute Node Positions (Layout)

Plotly does not compute graph layouts, so node positions must be generated first.

pos = nx.spring_layout(G)

Common layout algorithms:

Layout Idea
spring_layout force-directed layout
kamada_kawai_layout energy minimization
circular_layout circular geometry
spectral_layout Laplacian embedding

3. Build Edge Coordinates

Edges are drawn as line segments.


edge_x = []
edge_y = []

for u, v in G.edges():
x0, y0 = pos[u]
x1, y1 = pos[v]

edge_x += [x0, x1, None] edge_y += [y0, y1, None]

None separates edges so they are rendered as independent segments.

4. Create Edge Trace

Plotly trace representing all edges.

Edges are typically non-interactive lines.


import plotly.graph_objects as go

edge_trace = go.Scatter(
x=edge_x,
y=edge_y,
mode='lines',
line=dict(width=1),
hoverinfo='none'
)

5. Build Node Coordinates

Extract node positions from the layout.


node_x = []
node_y = []

for node in G.nodes():
x, y = pos[node]
node_x.append(x)
node_y.append(y)

Optional node attributes include:

  • degree
  • centrality
  • community group
  • labels

6. Create Node Trace

Nodes are rendered as markers.


node_trace = go.Scatter(
x=node_x,
y=node_y,
mode='markers',
marker=dict(size=15)
)

Common enhancements:

  • color nodes by degree
  • add hover text
  • display labels

7. Combine Traces into Figure


fig = go.Figure(
data=[edge_trace, node_trace]
)

8. Configure Layout

Remove axes and enable interaction.


fig.update_layout(
showlegend=False,
hovermode='closest',
xaxis=dict(visible=False),
yaxis=dict(visible=False)
)

9. Render Interactive Visualization


fig.show()

Plotly automatically enables:

  • zoom
  • pan
  • hover tooltips
  • image export

Example: NetworkX + Plotly

Code
import networkx as nx
import plotly.graph_objects as go

# -----------------------------
# 1. Build a sample graph
# -----------------------------
G = nx.Graph()

edges = [
    ("A", "B"), ("A", "C"), ("A", "D"),
    ("B", "C"), ("B", "E"),
    ("C", "F"), ("D", "E"),
    ("E", "F"), ("E", "G"),
    ("F", "H"), ("G", "H")
]
G.add_edges_from(edges)

# Example node attribute for coloring / hover
for node in G.nodes():
    G.nodes[node]["degree"] = G.degree[node]

# -----------------------------
# 2. Compute layout positions
# -----------------------------
pos = nx.spring_layout(G, seed=42)

# -----------------------------
# 3. Build edge traces
# -----------------------------
edge_x = []
edge_y = []

for u, v in G.edges():
    x0, y0 = pos[u]
    x1, y1 = pos[v]
    edge_x += [x0, x1, None]
    edge_y += [y0, y1, None]

edge_trace = go.Scatter(
    x=edge_x,
    y=edge_y,
    mode="lines",
    hoverinfo="none",
    line=dict(width=1)
)

# -----------------------------
# 4. Build node trace
# -----------------------------
node_x = []
node_y = []
node_text = []
node_color = []

for node in G.nodes():
    x, y = pos[node]
    deg = G.degree[node]
    node_x.append(x)
    node_y.append(y)
    node_text.append(f"Node: {node}<br>Degree: {deg}")
    node_color.append(deg)

node_trace = go.Scatter(
    x=node_x,
    y=node_y,
    mode="markers+text",
    text=list(G.nodes()),
    textposition="top center",
    hoverinfo="text",
    hovertext=node_text,
    marker=dict(
        size=22,
        color=node_color,
        colorbar=dict(title="Degree"),
        line=dict(width=1)
    )
)

# -----------------------------
# 5. Create figure
# -----------------------------
fig = go.Figure(
    data=[edge_trace, node_trace],
    layout=go.Layout(
        title="Interactive NetworkX + Plotly Graph",
        showlegend=False,
        hovermode="closest",
        margin=dict(l=20, r=20, t=50, b=20),
        xaxis=dict(showgrid=False, zeroline=False, visible=False),
        yaxis=dict(showgrid=False, zeroline=False, visible=False),
    )
)

fig.show()
Unable to display output for mime type(s): application/vnd.plotly.v1+json