使用matplotlib 与 numpy矩阵 的 3D 散点图(自用)

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值