summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorRoss Barnowski <rossbar@berkeley.edu>2020-10-24 10:34:56 -0700
committerGitHub <noreply@github.com>2020-10-24 10:34:56 -0700
commit2bf785b225ed57eff0a3322ae2df56bfe7bbb525 (patch)
treee3e737630a1361dbdb6a28c93be01889e5365b9c /examples
parent525b3c4aeb29a4ca397e61f91c282f6e97556114 (diff)
downloadnetworkx-2bf785b225ed57eff0a3322ae2df56bfe7bbb525.tar.gz
Add a 3D plotting example with matplotlib to the gallery (#4268)
* Add a 3D drawing example using matplotlib Based on the mayavi example, but using mpl_toolkits.mplot3d instead. * Rename to get sphinx gallery to pick up file * Change title.
Diffstat (limited to 'examples')
-rw-r--r--examples/3d_drawing/plot_basic.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/examples/3d_drawing/plot_basic.py b/examples/3d_drawing/plot_basic.py
new file mode 100644
index 00000000..75a9c79a
--- /dev/null
+++ b/examples/3d_drawing/plot_basic.py
@@ -0,0 +1,51 @@
+"""
+================
+Basic matplotlib
+================
+
+A basic example of 3D Graph visualization using `mpl_toolkits.mplot_3d`.
+
+"""
+
+import networkx as nx
+import numpy as np
+import matplotlib.pyplot as plt
+from mpl_toolkits.mplot3d import Axes3D
+
+# The graph to visualize
+G = nx.cycle_graph(20)
+
+# 3d spring layout
+pos = nx.spring_layout(G, dim=3, seed=779)
+# Extract node and edge positions from the layout
+node_xyz = np.array([pos[v] for v in sorted(G)])
+edge_xyz = np.array([(pos[u], pos[v]) for u, v in G.edges()])
+
+# Create the 3D figure
+fig = plt.figure()
+ax = fig.add_subplot(111, projection="3d")
+
+# Plot the nodes - alpha is scaled by "depth" automatically
+ax.scatter(*node_xyz.T, s=100, ec="w")
+
+# Plot the edges
+for vizedge in edge_xyz:
+ ax.plot(*vizedge.T, color="tab:gray")
+
+
+def _format_axes(ax):
+ """Visualization options for the 3D axes."""
+ # Turn gridlines off
+ ax.grid(False)
+ # Suppress tick labels
+ for dim in (ax.xaxis, ax.yaxis, ax.zaxis):
+ dim.set_ticks([])
+ # Set axes labels
+ ax.set_xlabel("x")
+ ax.set_ylabel("y")
+ ax.set_zlabel("z")
+
+
+_format_axes(ax)
+fig.tight_layout()
+plt.show()