gpt4 book ai didi

python-2.7 - 基于矩阵绘制等距图

转载 作者:行者123 更新时间:2023-12-04 03:16:44 26 4
gpt4 key购买 nike

如何从带数字的矩阵生成等轴测图?

我需要想法

例子:

矩阵:

[[3,2],

[1,1]]

这个 Isometric

每个数字都是高度,3代表3个立方体高度第一个文件,2代表2个立方体高度第一个文件第二个元素

谢谢

最佳答案

这是一个很好的问题。我认为 matplotlib 没有直接提供任何这样的功能,但我们当然可以通过它的六个表面来模拟立方体。获取这些表面我们可以使用提供的一段代码 here .然后我们需要在矩阵定义的位置绘制立方体。为了使绘图看起来是等距的,我们使用一种变通方法,在 mpl3d 轴的立方边界框的角上绘制不可见的点。最后,我们需要让坐标轴不可见。

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()

enter image description here

关于python-2.7 - 基于矩阵绘制等距图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40403956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com