import numpy as np #导入numpy库
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import math
########################################
# y = np.ones((4,4,4)) #新建三维数组,且初始值为1
# z = np.zeros((3,3))
# print(y) #输出y
# print(z) #输出y
# print(type(y)) #输出y的type
# print(type(z)) #输出y的type
# print(np.shape(y)) #输出y的大小
# print(np.shape(z)) #输出y的大小
# y[1:4, 1:4, 1:4] = 0
# print(y)
############################################
# 定义飞行器类 及其长宽高输出长宽高矩阵
#################
class Aircraft:
def __init__(self, aircraft_size):
self.l_X = aircraft_size[0]
self.l_Y = aircraft_size[1]
self.l_Z = aircraft_size[2]
self.space_martix(aircraft_size)
def space_martix(self, aircraft_size):
self.l_X = aircraft_size[0]
self.l_Y = aircraft_size[1]
self.l_Z = aircraft_size[2]
s_m = np.zeros((self.l_X, self.l_Y, self.l_Z))
s_m[:, :, :] = -1 #起点矩阵
return s_m
def main():
space_size = 10 #整个空域考虑的规格
aircraft_size = [4, 3, 3] # 可扩展编辑
A_s = Aircraft(aircraft_size) #申请对象
start_martix = A_s.space_martix(aircraft_size)
###dager space=3 useless_space=2 right_space=1 unknow_space=0 start point=-1
fig = plt.figure()
ax2 = plt.axes(projection='3d')
markers = ["o", "s", "D"]
colors = ["green", "red", "blue"]
y = np.zeros((space_size, space_size, space_size))
y[round(space_size/2), round(space_size/2), round(space_size/2)]=-1
# 替换中心 物体矩阵
y[round(round(space_size / 2, 1)-round(aircraft_size[0] / 2, 1)):round(round(space_size / 2, 1)-round(aircraft_size[0] / 2, 1))+aircraft_size[0], round(round(space_size / 2, 1)-round(aircraft_size[1] / 2, 1)):round(round(space_size / 2, 1)-round(aircraft_size[1] / 2, 1))+aircraft_size[1], round(round(space_size / 2, 1)-round(aircraft_size[2] / 2, 1)):round(round(space_size / 2, 1)-round(aircraft_size[2] / 2, 1))+aircraft_size[2]] = start_martix
# 选有效的点
for i in range(space_size):
for j in range(space_size):
for k in range(space_size):
if y[i, j, k] == 0 and i >= round(space_size / 2, 1) and (j-space_size / 2)**2+(k-space_size / 2)**2 <= (i-round(round(space_size / 2, 1)-round(aircraft_size[0] / 2, 1))+aircraft_size[0])**2:
y[i, j, k] = 1
elif y[i, j, k] == -1:
pass
else:
y[i, j, k] = 2
if y[i, j, k] == 1:
pass
ax2.scatter3D(0.01*i, 0.01*j, 0.01*k, s=20, c=colors[0], norm=0.75, marker=markers[0]) # 绘制散点图
elif y[i, j, k] == 2:
pass
ax2.scatter3D(0.01*i, 0.01*j, 0.01*k, s=20, c=colors[1], norm=0.75, marker=markers[1]) # 绘制散点图 print('final')
else:
pass
ax2.scatter3D(0.01*i, 0.01*j, 0.01*k, s=20, c=colors[2], norm=0.75, marker=markers[2])
print(y)
plt.show()
if __name__ == '__main__':
main()
使用matplotlib 与 numpy矩阵 的 3D 散点图(自用)
于 2022-10-12 15:36:28 首次发布