Last active
June 16, 2024 08:12
-
-
Save cvanelteren/33327965fc897810db30e20f25c6c076 to your computer and use it in GitHub Desktop.
data shader edge bundling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import proplot as plt, networkx as nx, pandas as pd | |
def bundle(g: nx.Graph, pos: dict): | |
from datashader.bundling import hammer_bundle | |
edges = [] | |
for u, v in g.edges(): | |
row = dict(source=u, target=v) | |
edges.append(row) | |
edges = pd.DataFrame(edges) | |
nodes = [] | |
for node in g.nodes(): | |
x, y = pos[node] | |
row = dict(name=node, x=x, y=y) | |
nodes.append(row) | |
nodes = pd.DataFrame(nodes) | |
return hammer_bundle(nodes, edges) | |
if __name__ == "__main__": | |
g = nx.fast_gnp_random_graph(100, 0.2) | |
pos = nx.circular_layout(g) | |
hb = bundle(g, pos) | |
fig, ax = plt.subplots() | |
nx.draw_networkx_nodes(g, pos, ax = ax) | |
ax.plot(hb.x, hb.y, color = "k") | |
ax.set_xlabel([]) | |
ax.set_ylabel([]) | |
ax.axis("equal") | |
ax.grid(False) | |
fig.show() | |
plt.show(block = 1) | |
Author
cvanelteren
commented
Apr 28, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment