Note
Go to the end to download the full example code.
三维旋转和随机游走的动画#
在三维谱布局中图的三维绘图动画示例。 参考 https://sphinx-gallery.github.io/stable/auto_examples/plot_8_animations.html 使用初始图的帧旋转动画,如 https://matplotlib.org/stable/api/animation_api.html 或完全帧重绘以在图上绘制随机游走。
在本地运行时,两个示例中都需要取消注释带有’plt.show()’的行。
import numpy as np
import networkx as nx
import random
import matplotlib.pyplot as plt
from matplotlib import animation
Define a graph to plot.#
Pick up a graph to look good in 3D.
Rotating 3D graph animation.#
In this example, a frame update is only a rotation of a given 3D graph.
def init():
ax.scatter(*nodes.T, alpha=0.2, s=100, color="blue")
for vizedge in edges:
ax.plot(*vizedge.T, color="gray")
ax.grid(False)
ax.set_axis_off()
plt.tight_layout()
return
def _frame_update(index):
ax.view_init(index * 0.2, index * 0.5)
return
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ani = animation.FuncAnimation(
fig,
_frame_update,
init_func=init,
interval=50,
cache_frame_data=False,
frames=100,
)
# plt.show()
Random walk on rotating 3D graph animation.#
The frame update can also draw a new plot in every frame giving the ultimate flexibility at the cost of performance loss.
def _frame_update(index):
ax.clear()
ax.scatter(*nodes.T, alpha=0.2, s=100, color="blue")
for vizedge in edges:
ax.plot(*vizedge.T, color="gray")
neighbors = list(G.neighbors(node[0]))
if index % 5 == 0:
node[0] = random.choice(neighbors)
node0 = nodes[node[0]]
ax.scatter(*node0, alpha=1, marker="s", color="red", s=100)
ax.view_init(index * 0.2, index * 0.5)
ax.grid(False)
ax.set_axis_off()
plt.tight_layout()
return
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.grid(False)
ax.set_axis_off()
plt.tight_layout()
node = [0]
ani = animation.FuncAnimation(
fig,
_frame_update,
interval=50,
cache_frame_data=False,
frames=100,
)
# plt.show()