networkx example

This commit is contained in:
Niko Abeler 2022-11-20 19:01:52 +01:00
parent 407a08d1f7
commit 46cac1528c
1 changed files with 18 additions and 0 deletions

View File

@ -22,4 +22,22 @@ import graph_force
edges = [(0, 1), (1, 2), (2, 3), (3, 0)] edges = [(0, 1), (1, 2), (2, 3), (3, 0)]
pos = graph_force.layout_from_edge_list(4, edges) pos = graph_force.layout_from_edge_list(4, edges)
```
### Example with networkx
```python
import networkx as nx
import graph_force
G = nx.grid_2d_graph(10, 10)
# we have to map the names to integers
# as graph_force only supports integers as node ids at the moment
edges = []
mapping = {n: i for i, n in enumerate(G.nodes)}
i = 0
for edge in G.edges:
edges.append((mapping[edge[0]], mapping[edge[1]]))
pos = graph_force.layout_from_edge_list(len(G.nodes), edges, iter=1000)
nx.draw(G, {n: pos[i] for n, i in mapping.items()}, node_size=2, width=0.1)
``` ```