如何解决以下问题的最佳解决方案?

时间:2019-08-11 14:15:24

标签: python matplotlib

我创建了一个名为grid的3D列表,并且创建了另一个列表,该列表的大小与之前的列表相同,称为零元素高程(这里的高程代表每个元素的高程= 1)。我给网格图= 1中的每个元素一个随机数,它代表一个高程。

我给了无人机高度= 60

如果这些随机数大于60 ...将它们画成黑色 如果这些随机数在50-60的范围内...以红色绘制它们 如果这些随机数小于50,则将其绘制为紫色

现在,我正在尝试绘制带有随机数(显示其高度)的1的网格和元素。请给我任何帮助?我该如何完成?

x represents a map with 
# 0 = navigable space
# 1 = occupied space
y the elvation of every 1 in x

我在下面编写了这段代码,但没有给我我想要的东西。我想在我的图形中将这些随机数及其高度显示为3D。

我在下面尝试:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import style
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

import random
import numpy as np

grid = [[[0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 1, 0]],
        [[0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 1, 0]],
        [[0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 1, 0]]]
risk = [i for i in range(50,60)]
init = [0,0,0]
goal = [1,4,5]
x= init[0]
y= init[1]
z= init[2]

x1= goal[0]
y1= goal[1]
z1= goal[2]
drone_height = 60
dxi=np.ones(1)
dyj=np.ones(1)
dzk=np.ones(1)
elevation = [[[0 for row in range(len(grid[0][0]))] for col in range(len(grid[0]))] for a in range(len(grid))]
for i in range(len(grid)):    
    for j in range(len(grid[0])):
        for k in range(len(grid[0][0])):

            if grid[i][j][k] == 1:
                elevation[i][j][k] = random.randint(1,100)
                if elevation[i][j][k] >= drone_height:
                    ax.bar3d(i, j, k, dxi, dyj,  dzk, color='black')
                elif elevation[i][j][k] in risk:
                    ax.bar3d(i, j, k, dxi, dyj,  dzk, color='red')
                else:
                    ax.bar3d(i, j, k, dxi, dyj,  dzk, color='purple')
            else:
                elevation[i][j][k] = 0

ax.scatter(x,y,z, color='g', marker='o')
ax.scatter(x1,y1,z1, color='b', marker='o')
print(elevation)
plt.show()

0 个答案:

没有答案