【问题标题】:fix graphic isometric figure修复图形等距图
【发布时间】:2016-11-08 05:16:24
【问题描述】:

我有以下代码取自Here,在此代码中,高度为 2 的图形由两个立方体(上下)生成,我想只用一个数字生成高度为 2 的图形,同样的数字高度 3,4,5,...

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

def cuboid_data(center, N, 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)]

    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 =      [[0,0,0,0,0],
         [N,N,N,N,N],    
         [0, 0, N,N, 0],
         [0, 0, N, N, 0]]
    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),N )
            ax.plot_surface(X, Y, Z, color='y', rstride=1, cstride=1)#,linewidth=0)

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([[2,2],[1,2]])
    plotIsoMatrix(ax, matrix)
    #ax.set_axis_off()
    plt.ion()
    plt.show()

这样生成下图:

我要生成下图:

如何解决这个问题?

谢谢

【问题讨论】:

  • 在我在这里给出一个新的答案之前,你能不能先接受the other post的答案,因为它显然是合适的?!
  • @ImportanceOfBeingErnest 好的,我是新来的

标签: python python-2.7 matplotlib


【解决方案1】:

使用matplotlib的解决方案

首先,如果你不想有单个立方体,而是长方体,有一个更简单的解决方案 - 使用 matplotlib bar3d

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

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_aspect('equal')
matrix = np.array([[2,2],[1,2]])
xpos, ypos = np.meshgrid(np.arange(matrix.shape[0]),np.arange(matrix.shape[1]) )
xpos = xpos.flatten('F')
ypos = ypos.flatten('F')
zpos = np.zeros_like(xpos)
dx = np.ones_like(zpos)
dy = dx.copy()
dz = matrix.flatten()

ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='y', zsort='average', linewidth=0)

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)
ax.set_axis_off()      
plt.show()

关于长方体的重叠面,matplotlib 中没有解决方案。我认为这种行为通常被认为是一个无法解决的错误,正如Matplotlib 3D FAQ 中所描述的那样。此外,手动设置 zorder 将不起作用。然而,好消息是,这种重叠取决于角度。所以你总能找到一个看起来不错的视角(用鼠标旋转绘图)。

使用 mayavi 的解决方案

使用 Mayavi,完全没有人脸重叠的问题。此外,mayavi 中的barchart 更方便,只需 5 行代码:

import numpy as np
import mayavi.mlab as mlab
mlab.figure(bgcolor=(1,1,1))
matrix = np.array([[2,2],[1,2]])
mlab.barchart(matrix, color=(1.,0.86, 0.12), lateral_scale=1.0)
mlab.show()

【讨论】:

    猜你喜欢
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2018-01-09
    相关资源
    最近更新 更多