Mayavi2#

import networkx as nx
import numpy as np
from mayavi import mlab

# 一些尝试的图表
# H = nx.krackhardt_kite_graph()
# H=nx.Graph();H.add_edge('a','b');H.add_edge('a','c');H.add_edge('a','d')
# H = nx.grid_2d_graph(4, 5)
H = nx.cycle_graph(20)

# 将节点重新排序从0到len(G)-1
G = nx.convert_node_labels_to_integers(H)
# 三维弹簧布局
pos = nx.spring_layout(G, dim=3, seed=1001)
# 按节点顺序排列的x,y,z位置的numpy数组
xyz = np.array([pos[v] for v in sorted(G)])
# 标量颜色
scalars = np.array(list(G.nodes())) + 5

mlab.figure()

pts = mlab.points3d(
    xyz[:, 0],
    xyz[:, 1],
    xyz[:, 2],
    scalars,
    scale_factor=0.1,
    scale_mode="none",
    colormap="Blues",
    resolution=20,
)

pts.mlab_source.dataset.lines = np.array(list(G.edges()))
tube = mlab.pipeline.tube(pts, tube_radius=0.01)
mlab.pipeline.surface(tube, color=(0.8, 0.8, 0.8))
mlab.orientation_axes()

Gallery generated by Sphinx-Gallery