根据矩阵绘制等轴测图 [英] Draw Isometric figure based on matrix

查看:30
本文介绍了根据矩阵绘制等轴测图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从具有数字的矩阵中生成等轴测图?

我需要想法

示例:

矩阵:

[[3,2],

[1,1]]

每个数字是高度,3表示3个立方体的高度的第一个文件,2表示2个立方体的高度的第一个文件的第二个

谢谢

解决方案

这是一个很好的问题.我认为matplotlib不会直接提供任何此类功能,但是我们当然可以通过其六个表面来模拟多维数据集.获取这些表面,我们可以使用

How can generate a isometric from a matrix with numbers??

I need ideas

Example:

Matrix:

[[3,2],

[1,1]]

This

Each number is the height, 3 represent 3 cubes height first file, 2 represent 2 cubes height first file second element

Thanks

解决方案

This is a very nice question. I think matplotlib does not directly provide any such function, but we can of course simulate a cube by its six surfaces. Getting those surfaces we can use a piece of code provided here. We then need to plot the cube at the positions defined by the matrix. In order to make the plot look isometric, we use a workaround, plotting invisible points at the corners of the cubic bounding box of the mpl3d axes. Finally we need to make the axes invisible.

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

def cuboid_data(center, size=(1,1,1)):
    # code taken from
    # https://stackoverflow.com/questions/30715083/python-plotting-a-wireframe-3d-cuboid?noredirect=1&lq=1
    # suppose axis direction: x: to left; y: to inside; z: to upper
    # get the (left, outside, bottom) point
    o = [a - b / 2 for a, b in zip(center, size)]
    # get the length, width, and height
    l, w, h = size
    x = [[o[0], o[0] + l, o[0] + l, o[0], o[0]],  # x coordinate of points in bottom surface
         [o[0], o[0] + l, o[0] + l, o[0], o[0]],  # x coordinate of points in upper surface
         [o[0], o[0] + l, o[0] + l, o[0], o[0]],  # x coordinate of points in outside surface
         [o[0], o[0] + l, o[0] + l, o[0], o[0]]]  # x coordinate of points in inside surface
    y = [[o[1], o[1], o[1] + w, o[1] + w, o[1]],  # y coordinate of points in bottom surface
         [o[1], o[1], o[1] + w, o[1] + w, o[1]],  # y coordinate of points in upper surface
         [o[1], o[1], o[1], o[1], o[1]],          # y coordinate of points in outside surface
         [o[1] + w, o[1] + w, o[1] + w, o[1] + w, o[1] + w]]    # y coordinate of points in inside surface
    z = [[o[2], o[2], o[2], o[2], o[2]],                        # z coordinate of points in bottom surface
         [o[2] + h, o[2] + h, o[2] + h, o[2] + h, o[2] + h],    # z coordinate of points in upper surface
         [o[2], o[2], o[2] + h, o[2] + h, o[2]],                # z coordinate of points in outside surface
         [o[2], o[2], o[2] + h, o[2] + h, o[2]]]                # z coordinate of points in inside surface
    return x, y, z

def plotCubeAt(pos=(0,0), N=0, ax=None):
    # Plotting N cube elements at position pos
    if ax !=None:
        if N > 0:
            for n in range(N):
                X, Y, Z = cuboid_data( (pos[0],pos[1],n) )
                ax.plot_surface(X, Y, Z, color='b', rstride=1, cstride=1, alpha=1)

def plotIsoMatrix(ax, matrix):
    # plot a Matrix 
    # where matrix[i,j] cubes are added at position (i,j) 
    for i  in range(matrix.shape[0]):
            for j in range(matrix.shape[1]):
                plotCubeAt(pos=(i,j), N=matrix[i,j], ax=ax)

    l = max(matrix.shape[0], matrix.shape[1], matrix.max())
    bb = np.array([(0,0,0), (0,l,0), (l,0,0), (l,l,0),(0,0,l), (0,l,l), (l,0,l), (l,l,l)])
    ax.plot(bb[:,0], bb[:,1], bb[:,2], "w", alpha=0.0)            



if __name__ == '__main__':
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.set_aspect('equal')
    matrix = np.array([[3,2],[1,1]])
    plotIsoMatrix(ax, matrix)
    ax.set_axis_off()
    plt.show()

这篇关于根据矩阵绘制等轴测图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆