1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Basic matplotlib\n\nA basic example of 3D Graph visualization using `mpl_toolkits.mplot_3d`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import networkx as nx\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\n\n# The graph to visualize\nG = nx.cycle_graph(20)\n\n# 3d spring layout\npos = nx.spring_layout(G, dim=3, seed=779)\n# Extract node and edge positions from the layout\nnode_xyz = np.array([pos[v] for v in sorted(G)])\nedge_xyz = np.array([(pos[u], pos[v]) for u, v in G.edges()])\n\n# Create the 3D figure\nfig = plt.figure()\nax = fig.add_subplot(111, projection=\"3d\")\n\n# Plot the nodes - alpha is scaled by \"depth\" automatically\nax.scatter(*node_xyz.T, s=100, ec=\"w\")\n\n# Plot the edges\nfor vizedge in edge_xyz:\n ax.plot(*vizedge.T, color=\"tab:gray\")\n\n\ndef _format_axes(ax):\n \"\"\"Visualization options for the 3D axes.\"\"\"\n # Turn gridlines off\n ax.grid(False)\n # Suppress tick labels\n for dim in (ax.xaxis, ax.yaxis, ax.zaxis):\n dim.set_ticks([])\n # Set axes labels\n ax.set_xlabel(\"x\")\n ax.set_ylabel(\"y\")\n ax.set_zlabel(\"z\")\n\n\n_format_axes(ax)\nfig.tight_layout()\nplt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|