Draw interactive NetworkX graphs with Altair
Project description
altair-nx
Draw interactive NetworkX graphs with Altair
altair-nx offers a similar draw API to NetworkX but returns Altair Charts instead.
This project started as a fork of nx-altair (inactive since 2020) meant for fixing issues and merging pull requests, but after a full code-base rework (causing backward and forward incompatibility) and the implementation of new features (e.g. curved edges, self loops, and much greater customisation), it became reasonable for it to be its own library.
Installation:
pip install altair-nx
Examples
Every function argument is explained in the documentation (and most IDEs will pull from it on hover or for auto-complete), but the simplest starting point for altair-nx is playing around with the tutorial notebook.
PS: if you draw something cool or which could be a good example of using combinations of the various features, feel free to put it in a notebook and open a pull request with it added to the examples folder.
Simple graph
import networkx as nx
import altair_nx as anx
# Generate a random graph
G = nx.fast_gnp_random_graph(n = 20, p = 0.25)
# Compute node positions
pos = nx.spring_layout(G)
# Draw the graph with altair-nx
viz = anx.draw_networkx(G, pos)
# Display it and make it interactive
viz.interactive()
Minimal customisation
Many of altair-nx's style configuration arguments (most being similar to NetworkX's) support declarative mapping to node and edge attributes (e.g. colours, shapes, and sizes). Again, see the documentation or the tutorial notebook, but here is a simple styling of the graph from above:
import numpy as np
# Add attributes to nodes and edges
for n in G.nodes(): G.nodes[n]['importance'] = np.random.randn()
for e in G.edges(): G.edges[e]['weight'] = np.random.uniform(1, 10)
# Draw the graph with altair-nx
viz = anx.draw_networkx(G, pos,
node_colour = 'importance', node_cmap = 'viridis',
edge_colour = 'black', edge_width = 'weight', curved_edges = True
)
# Display it and make it interactive
viz.interactive()
Leverage Altair for deeper interactivity
Make a chart depend on a selection over the graph:
Create controls to affect chart parameters:
Both examples are in the tutorial notebook.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file altair_nx-1.3.tar.gz
.
File metadata
- Download URL: altair_nx-1.3.tar.gz
- Upload date:
- Size: 2.7 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97d846030d3fe3a5acbd7042f3907db249087d0c45c755828399c5b71af1ebeb |
|
MD5 | 41750554e9eff5d245ad8010d4f6c111 |
|
BLAKE2b-256 | e118518fa7ee428744027b3a8860a50c31c202c3e931fe33810b0451a81d02ac |
File details
Details for the file altair_nx-1.3-py3-none-any.whl
.
File metadata
- Download URL: altair_nx-1.3-py3-none-any.whl
- Upload date:
- Size: 18.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99b96b092baa07f589ff7126c8a2725e22ebd51cf38ac039d92f4e861efe50a4 |
|
MD5 | fd6c8057af02bd9c425516464a284893 |
|
BLAKE2b-256 | 461684b481529284f2288edf180d08622cb34e8ff4b19fd045c531afddf28767 |