diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/graph/plot_words.py | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/examples/graph/plot_words.py b/examples/graph/plot_words.py index ccaaebb7..9b4eb42a 100644 --- a/examples/graph/plot_words.py +++ b/examples/graph/plot_words.py @@ -19,6 +19,7 @@ The data file can be found at: import gzip from string import ascii_lowercase as lowercase +import matplotlib.pyplot as plt import networkx as nx @@ -67,8 +68,25 @@ print(f"{nx.number_connected_components(G)} connected components") for (source, target) in [("chaos", "order"), ("nodes", "graph"), ("pound", "marks")]: print(f"Shortest path between {source} and {target} is") try: - sp = nx.shortest_path(G, source, target) - for n in sp: + shortest_path = nx.shortest_path(G, source, target) + for n in shortest_path: print(n) except nx.NetworkXNoPath: print("None") + + +# draw a subset of the graph +boundary = list(nx.node_boundary(G, shortest_path)) +G.add_nodes_from(shortest_path, color="red") +G.add_nodes_from(boundary, color="blue") +H = G.subgraph(shortest_path + boundary) +colors = nx.get_node_attributes(H, "color") +options = { + "node_size": 1500, + "alpha": 0.3, + "node_color": colors.values(), +} +pos = nx.kamada_kawai_layout(H) +nx.draw(H, pos, **options) +nx.draw_networkx_labels(H, pos, font_weight="bold") +plt.show() |