每个顶点的python matplotpib网格颜色图

时间:2019-05-27 08:49:22

标签: python matlab

是否可以使用matplotlib Axes3D来明确指定每个顶点的颜色?
使用matlab和以下代码,我设法生成了
以下图:
enter image description here

    function a = visualize_result(log_dir)
    %% Visualize Supervised Network Results

    mesh_0 = load('1.mat');
    mesh_1 = load('2.mat');
    cd ..
    X = load(curr.mat'));
    cd misc/
    colors = create_colormap(mesh_1,mesh_1);
    figure;
    subplot(1,2,1); colormap(colors);
    plot_scalar_map(mesh_1,[1: size(mesh_1.VERT,1)]');freeze_colors;title('Target');
    view(0,90)
    subplot(1,2,2); colormap(colors(X,:));
    plot_scalar_map(mesh_0,[1: size(mesh_0.VERT,1)]');freeze_colors;title('Source');
    view(0,90)



    function colors = create_colormap(M,N)

    % create colormap
    minx = min(min(M.VERT(:,1)),min(N.VERT(:,1)));
    miny = min(min(M.VERT(:,2)),min(N.VERT(:,2)));
    minz = min(min(M.VERT(:,3)),min(N.VERT(:,3)));
    maxx = max(max(M.VERT(:,1)),max(N.VERT(:,1)));
    maxy = max(max(M.VERT(:,2)),max(N.VERT(:,2)));
    maxz = max(max(M.VERT(:,3)),max(N.VERT(:,3)));
    colors = [(M.VERT(:,1)-minx)/(maxx-minx) (M.VERT(:,2)-miny)/(maxy-miny) (M.VERT(:,3)-minz)/(maxz-minz)];

    end

我尝试将其导入matplotlib,结果如下

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import scipy.io as sio
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

def plot_compare_meshes(mesh_1_path,mesh_2_path,):
    fig = plt.figure()
    ax1 = fig.add_subplot(121, projection='3d'); ax1.view_init(90, -90)

    mesh_1 = sio.loadmat(mesh_1_path)
    X_1 = mesh_1['VERT'][:, 0]
    Y_1 = mesh_1['VERT'][:, 1]
    Z_1 = mesh_1['VERT'][:, 2]
    TRIV_1 = mesh_1['TRIV'] - 1

    colormap = create_colormap(mesh_1['VERT'])


    ax1.plot_trisurf(X_1, Y_1, Z_1, triangles=TRIV_1,color=colormap)

    plt.show()

def create_colormap(VERT):
    VERT = np.double(VERT)
    minx = np.min(VERT[:,0])
    miny = np.min(VERT[:,1])
    minz = np.min(VERT[:,2])
    maxx = np.max(VERT[:,0])
    maxy = np.max(VERT[:,1])
    maxz = np.max(VERT[:,2])
    colors = list(zip(
        ((VERT[:,0] - minx) / (maxx - minx)),
        ((VERT[:, 1] - miny) / (maxy - miny)),
        ((VERT[:, 2] - minz) / (maxz - minz))))
    return colors






if __name__ == "__main__":
    plot_compare_meshes(mesh1,mesh2)


enter image description here 从图像中可以看出,我认为我丢失了一些东西,因为绘图根本不平滑,我想知道我是否在调用正确的xargs。
我需要使用自己的颜色图,而不要使用默认的图。 (这些颜色后来被排列并绘制了不同的形状。

0 个答案:

没有答案