如何在Mayavi中为3d图形的边缘着色?

问题描述 投票:1回答:1

我有一个使用Mayavi创建的3d图,其边缘必须用标量值着色。

下面的代码创建图形,但是我不确定如何给边缘着色

import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
from mayavi import mlab

def main(edge_color=(0.8, 0.8, 0.8), edge_size=0.02):

    t = [1, 2, 4, 4, 5, 3, 5]
    h = [2, 3, 6, 5, 6, 4, 1]


    ed_ls = [(x, y) for x, y in zip(t, h)]
    G = nx.OrderedGraph()
    G.add_edges_from(ed_ls)
    nx.draw(G)
    plt.show()

    graph_pos = nx.spring_layout(G, dim=3)

    # numpy array of x,y,z positions in sorted node order
    xyz = np.array([graph_pos[v] for v in sorted(G)])
    mlab.figure(1)
    mlab.clf()
    pts = mlab.points3d(xyz[:, 0], xyz[:, 1], xyz[:, 2])
    pts.mlab_source.dataset.lines = np.array(G.edges())
    tube = mlab.pipeline.tube(pts, tube_radius=edge_size)
    mlab.pipeline.surface(tube, color=edge_color)

    mlab.show()  # interactive window

main()

用于着色边缘的标量值

scalar = [0.1, 0.7, 0.3, 0.5, 0.9, 0.8, 0.2]

关于如何执行此操作的任何建议都会很有帮助。

我还在已创建的3d图形中看到另一个问题。边缘之一未连接到节点。

enter image description here

编辑:据我了解,mlab.pipeline.surface(tube,color = edge_color)用于为边缘/管着色。

更新的代码:

def main(edge_color=(0.8, 0.2, 0.8), edge_size=0.02, graph_colormap='winter'):
    t = [1, 2, 4, 4, 5, 3, 5]
    h = [2, 3, 6, 5, 6, 4, 1]

    ed_ls = [(x, y) for x, y in zip(t, h)]
    G = nx.OrderedGraph()
    G.add_edges_from(ed_ls)
    nx.draw(G)
    plt.show()
    scalars = np.array(G.nodes())+5
    pprint(scalars)
    e_color = [(0.8, 0.2, 0.8), (0.8, 0.2, 0.8), (0.8, 0.2, 0.8),
               (0.8, 0.2, 0.8), (0.8, 0.2, 0.8), (0.8, 0.2, 0.8),
               (0.8, 0.2, 0.8)]
    graph_pos = nx.spring_layout(G, dim=3)

    # numpy array of x,y,z positions in sorted node order
    xyz = np.array([graph_pos[v] for v in sorted(G)])
    mlab.figure(1)
    mlab.clf()
    pts = mlab.points3d(xyz[:, 0], xyz[:, 1], xyz[:, 2],
                        scalars,
                        colormap=graph_colormap
                        )
    pts.mlab_source.dataset.lines = np.array(G.edges())
    tube = mlab.pipeline.tube(pts, tube_radius=edge_size)
    #mlab.pipeline.surface(tube, color=e_color)  # doesn't work
    mlab.pipeline.surface(tube, color=edge_color)  # doesn't work

    mlab.show()  # interactive window

但是问题是我无法为不同的边缘/管分配不同的颜色

python-3.x 3d networkx mayavi mayavi.mlab
1个回答
0
投票

[一种可能的解决方案,完全不是自动化的,但足以证明概念。

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

t = [1, 2, 4, 4, 5, 3, 5]
h = [2, 3, 6, 5, 6, 4, 1]
ed_ls = [(x, y) for x, y in zip(t, h)]
G = nx.OrderedGraph()
G.add_edges_from(ed_ls)
graph_pos = nx.spring_layout(G, dim=3)
xyz = np.array([graph_pos[v] for v in G])
print(xyz.shape)
mlab.points3d(xyz[:, 0], xyz[:, 1], xyz[:, 2],
              np.linspace(1, 2, xyz.shape[0]),
              colormap='winter', resolution=100, scale_factor=0.3)
smallTri = np.tile(xyz[-3:, :], (2, 1))[:4, :]
remEdges = np.vstack((xyz[-1, :], xyz[:-2, :]))
allEdges = np.vstack((smallTri, remEdges))
for i in range(allEdges.shape[0] - 1):
    mlab.plot3d(allEdges[i:i + 2, 0], allEdges[i:i + 2, 1],
                allEdges[i:i + 2, 2], color=(0.2, 1 - 0.1 * i, 0.8))
mlab.show()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.