summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJarrod Millman <jarrod.millman@gmail.com>2020-10-26 22:16:39 -0700
committerGitHub <noreply@github.com>2020-10-26 22:16:39 -0700
commit44e805a8f6387be1317258257dc6e37087c41183 (patch)
tree17eaff4543fa11b4dce71ea667ab4650be9f9f8d /examples
parente83c2eed698d9b4ab3056fd8ebd88e97e41645d3 (diff)
downloadnetworkx-44e805a8f6387be1317258257dc6e37087c41183.tar.gz
Add simple graph w/ manual layout (#4291)
* Draw simple graph w/ manual layout * Add example from Dan * Move per Ross suggestion
Diffstat (limited to 'examples')
-rw-r--r--examples/basic/plot_simple_graph.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/examples/basic/plot_simple_graph.py b/examples/basic/plot_simple_graph.py
new file mode 100644
index 00000000..fcd7caf6
--- /dev/null
+++ b/examples/basic/plot_simple_graph.py
@@ -0,0 +1,60 @@
+"""
+============
+Simple graph
+============
+
+Draw simple graph with manual layout.
+"""
+
+import networkx as nx
+import matplotlib.pyplot as plt
+
+G = nx.Graph()
+G.add_edge(1, 2)
+G.add_edge(1, 3)
+G.add_edge(1, 5)
+G.add_edge(2, 3)
+G.add_edge(3, 4)
+G.add_edge(4, 5)
+
+# explicitly set positions
+pos = {1: (0, 0), 2: (-1, 0.3), 3: (2, 0.17), 4: (4, 0.255), 5: (5, 0.03)}
+
+options = {
+ "font_size": 36,
+ "node_size": 3000,
+ "node_color": "white",
+ "edgecolors": "black",
+ "linewidths": 5,
+ "width": 5,
+}
+nx.draw_networkx(G, pos, **options)
+
+# Set margins for the axes so that nodes aren't clipped
+ax = plt.gca()
+ax.margins(0.20)
+plt.axis("off")
+plt.show()
+
+# %%
+# A directed graph
+
+G = nx.DiGraph([(0, 3), (1, 3), (2, 4), (3, 5), (3, 6), (4, 6), (5, 6)])
+
+# group nodes by column
+left_nodes = [0, 1, 2]
+middle_nodes = [3, 4]
+right_nodes = [5, 6]
+
+# set the position according to column (x-coord)
+pos = {n: (0, i) for i, n in enumerate(left_nodes)}
+pos.update({n: (1, i + 0.5) for i, n in enumerate(middle_nodes)})
+pos.update({n: (2, i + 0.5) for i, n in enumerate(right_nodes)})
+
+nx.draw_networkx(G, pos, **options)
+
+# Set margins for the axes so that nodes aren't clipped
+ax = plt.gca()
+ax.margins(0.20)
+plt.axis("off")
+plt.show()