如何在mayavi中绘制3D图形/树并仅在其节点的特定子集之间绘制边缘?

时间:2019-08-30 09:38:29

标签: python networkx mayavi graph-visualization mayavi.mlab

我正在尝试绘制一些表示神经元的数据:沿着神经元的节点被视为树/图的节点,并且对应于某些设备以um为单位的测量值。我也知道这些节点的哪些对被连接以及节点3D坐标。但是,由于节点和边缘的数量很多,如果我在一个循环中一一渲染每个边缘,则mayavi在渲染时会卡住。

我设法用matplotlib做到了,但是渲染速度超级慢。我知道使用nertworkx来存储图形和mayavi进行可视化是一个更好的选择,但是我在使用这种方法时也遇到了一些困难。我看到了一些类似这样的示例并且也尝试过:

# reorder nodes from 0,len(G)-1
G=nx.convert_node_labels_to_integers(g)

# 3d spring layout
pos=nx.spring_layout(G,dim=3)

scalars=np.array(G.nodes())+5

mlab.figure(1, bgcolor=(0, 0, 0))
mlab.clf()

# plot the nodes
pts = mlab.points3d(x_coords, y_coords, z_coords,
                    scalars,
                    scale_factor=0.5,
                    scale_mode='none',
                    colormap='Blues',
                    resolution=20)

# plot the edges
pts.mlab_source.dataset.lines = np.array(G.edges())

mlab.pipeline.surface(pts, color=(1, 1, 1),
                              representation='wireframe',
                              line_width=4,
                              name='Connections')
tube = mlab.pipeline.tube(pts, tube_radius=0.5)
mlab.pipeline.surface(tube, color=(0.8, 0.8, 0.8))
mlab.show()

这不能按预期工作,因为它在所有节点之间渲染了太多的边缘,而不是像树一样的结构,我得到了更多的全连接图。但是,G.edges()仅包含我知道存在的边缘,因此我不知道渲染的额外边缘来自何处。

我还尝试在循环中渲染每个边缘(这也适用于matplotlib)。我认为这在原则上是可行的,但是由于节点和边缘的数量过多,该程序被卡住了:

# reorder nodes from 0,len(G)-1
G=nx.convert_node_labels_to_integers(g)
# 3d spring layout
pos=nx.spring_layout(G,dim=3)

scalars=np.array(G.nodes())+5

mlab.figure(1, bgcolor=(0, 0, 0))
mlab.clf()

pts = mlab.points3d(x_coords, y_coords, z_coords,
                    scalars,
                    scale_factor=0.5,
                    scale_mode='none',
                    colormap='Blues',
                    resolution=20)

for el in list(g.edges()):
    x = np.array([x_coords[nodes_ids.tolist().index(el[0])], x_coords[nodes_ids.tolist().index(el[1])]])
    y = np.array([y_coords[nodes_ids.tolist().index(el[0])], y_coords[nodes_ids.tolist().index(el[1])]])
    z = np.array([z_coords[nodes_ids.tolist().index(el[0])], z_coords[nodes_ids.tolist().index(el[1])]])

    mlab.plot3d(x, y, z)

mlab.show()

我期望的是一个看起来像神经元的结构。当我仅渲染节点时,我可以看到它具有正确的形状(基本上是神经元的形状)。但是,在3D中仅将正确的节点与边缘连接似乎是一个问题。

0 个答案:

没有答案