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 图中看到了另一个问题。其中一条边未连接到节点。

在此处输入图像描述

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

更新代码:

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

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

4

1 回答 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()

在此处输入图像描述

于 2020-04-29T05:55:24.573 回答