Skip to content

Instantly share code, notes, and snippets.

@cvanelteren
Last active June 16, 2024 08:12
Show Gist options
  • Save cvanelteren/33327965fc897810db30e20f25c6c076 to your computer and use it in GitHub Desktop.
Save cvanelteren/33327965fc897810db30e20f25c6c076 to your computer and use it in GitHub Desktop.
data shader edge bundling
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)
@cvanelteren
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment