summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJarrod Millman <jarrod.millman@gmail.com>2020-12-02 22:57:30 -0800
committerGitHub <noreply@github.com>2020-12-02 22:57:30 -0800
commit1bbe6aaf473609e7b9466d01dc02027bf304d5ca (patch)
tree3bde2be2dd18947a823c77962145a49d2152c2ab /examples
parent58c5252604df7110d634c094cd9c05dfbdb0fdc6 (diff)
downloadnetworkx-1bbe6aaf473609e7b9466d01dc02027bf304d5ca.tar.gz
Add words graph plot (#4409)
Diffstat (limited to 'examples')
-rw-r--r--examples/graph/plot_words.py22
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()