第一种方法
| from mpl_toolkits.mplot3d import Axes3D |
| from mpl_toolkits.mplot3d.art3d import Poly3DCollection |
| import numpy as np |
| import matplotlib.pyplot as plt |
| from matplotlib.pyplot import MultipleLocator |
| |
| |
| def cuboid_data2(o, size=(10, 10, 10)): |
| X = [[[0, 1, 0], [0, 0, 0], [1, 0, 0], [1, 1, 0]], |
| [[0, 0, 0], [0, 0, 1], [1, 0, 1], [1, 0, 0]], |
| [[1, 0, 1], [1, 0, 0], [1, 1, 0], [1, 1, 1]], |
| [[0, 0, 1], [0, 0, 0], [0, 1, 0], [0, 1, 1]], |
| [[0, 1, 0], [0, 1, 1], [1, 1, 1], [1, 1, 0]], |
| [[0, 1, 1], [0, 0, 1], [1, 0, 1], [1, 1, 1]]] |
| X = np.array(X).astype(float) |
| for i in range(3): |
| X[:, :, i] *= size[i] |
| X += np.array(o) |
| return X |
| |
| |
| def plotCubeAt2(positions, sizes=None, colors=None, **kwargs): |
| if not isinstance(colors, (list, np.ndarray)): |
| colors = ["C0"] * len(positions) |
| |
| if not isinstance(sizes, (list, np.ndarray)): |
| sizes = [(1, 1, 1)] * len(positions) |
| g = [] |
| for p, s, c in zip(positions, sizes, colors): |
| g.append(cuboid_data2(p, size=s)) |
| return Poly3DCollection(np.concatenate(g), facecolors=np.repeat(colors, 6), alpha=0.6, **kwargs) |
| |
| |
| positions = [(50, 50, 50), (10, 10, 10)] |
| sizes = [(20, 20, 20), (20, 20, 20)] |
| colors = ["Gray", "Gray"] |
| |
| fig = plt.figure() |
| ax = fig.add_subplot(111, projection="3d") |
| ax.set_aspect('equal') |
| |
| pc = plotCubeAt2(positions, sizes, colors=colors, edgecolor="k") |
| ax.add_collection3d(pc) |
| |
| ax.set_xlim([0, 100]) |
| ax.set_ylim([0, 100]) |
| ax.set_zlim([0, 100]) |
| |
| |
| |
| major_locator = MultipleLocator(10) |
| ax.xaxis.set_major_locator(major_locator) |
| ax.yaxis.set_major_locator(major_locator) |
| ax.zaxis.set_major_locator(major_locator) |
| |
| plt.show() |
效果图

第二种方法
| from mpl_toolkits.mplot3d import Axes3D |
| import numpy as np |
| import matplotlib.pyplot as plt |
| |
| def cuboid_data(o, size=(1,1,1)): |
| |
| |
| |
| |
| l, w, h = size |
| x = [[o[0], o[0] + l, o[0] + l, o[0], o[0]], |
| [o[0], o[0] + l, o[0] + l, o[0], o[0]], |
| [o[0], o[0] + l, o[0] + l, o[0], o[0]], |
| [o[0], o[0] + l, o[0] + l, o[0], o[0]]] |
| y = [[o[1], o[1], o[1] + w, o[1] + w, o[1]], |
| [o[1], o[1], o[1] + w, o[1] + w, o[1]], |
| [o[1], o[1], o[1], o[1], o[1]], |
| [o[1] + w, o[1] + w, o[1] + w, o[1] + w, o[1] + w]] |
| z = [[o[2], o[2], o[2], o[2], o[2]], |
| [o[2] + h, o[2] + h, o[2] + h, o[2] + h, o[2] + h], |
| [o[2], o[2], o[2] + h, o[2] + h, o[2]], |
| [o[2], o[2], o[2] + h, o[2] + h, o[2]]] |
| return np.array(x), np.array(y), np.array(z) |
| |
| |
| def plotCubeAt(pos=(0,0,0), size=(1,1,1), ax=None,**kwargs): |
| |
| if ax !=None: |
| X, Y, Z = cuboid_data( pos, size ) |
| ax.plot_surface(X, Y, Z, rstride=1, cstride=1, **kwargs) |
| |
| |
| positions = [(-3,5,-2),(1,7,1)] |
| sizes = [(4,5,3), (3,3,7)] |
| colors = ["crimson","limegreen"] |
| |
| |
| fig = plt.figure() |
| ax = fig.add_subplot(111, projection="3d") |
| ax.set_aspect('equal') |
| |
| for p,s,c in zip(positions,sizes,colors): |
| plotCubeAt(pos=p, size=s, ax=ax, color=c) |
| |
| plt.show() |
效果图

第三种方法
| |
| import numpy as np |
| import matplotlib.pyplot as plt |
| from mpl_toolkits.mplot3d import Axes3D |
| from matplotlib.pyplot import MultipleLocator |
| |
| |
| length = 10 |
| width = 10 |
| height = 10 |
| |
| |
| matrix = np.zeros((length, width, height)) |
| |
| |
| obstacle_length = 2 |
| obstacle_width = 2 |
| obstacle_height = 3 |
| obstacle_pos = (1, 1, 1) |
| |
| |
| matrix[obstacle_pos[0]:obstacle_pos[0]+obstacle_length, |
| obstacle_pos[1]:obstacle_pos[1]+obstacle_width, |
| obstacle_pos[2]:obstacle_pos[2]+obstacle_height] = 1 |
| |
| |
| fig = plt.figure() |
| ax = fig.add_subplot(111, projection='3d') |
| |
| |
| indexes = np.where(matrix != 0) |
| |
| |
| ax.scatter(indexes[0], indexes[1], indexes[2], c='red') |
| |
| |
| ax.set_xlabel('X') |
| ax.set_ylabel('Y') |
| ax.set_zlabel('Z') |
| |
| |
| ax.set_xlim([0, length-1]) |
| ax.set_ylim([0, width-1]) |
| ax.set_zlim([0, height-1]) |
| |
| |
| major_locator = MultipleLocator(1) |
| ax.xaxis.set_major_locator(major_locator) |
| ax.yaxis.set_major_locator(major_locator) |
| ax.zaxis.set_major_locator(major_locator) |
| |
| |
| plt.show() |
效果图

第四种方法
| import numpy as np |
| import matplotlib.pyplot as plt |
| vertices = np.array([ |
| [0, 0, 0], |
| [1, 0, 0], |
| [1, 1, 0], |
| [0, 1, 0], |
| [0, 0, 1], |
| [1, 0, 1], |
| [1, 1, 1], |
| [0, 1, 1] |
| ]) |
| |
| edges = np.array([ |
| [0, 1], |
| [1, 2], |
| [2, 3], |
| [3, 0], |
| [4, 5], |
| [5, 6], |
| [6, 7], |
| [7, 4], |
| [0, 4], |
| [1, 5], |
| [2, 6], |
| [3, 7] |
| ]) |
| |
| fig = plt.figure() |
| ax = fig.add_subplot(111, projection="3d") |
| |
| for i, vertex in enumerate(vertices): |
| ax.scatter(vertex[0], vertex[1], vertex[2], c="c", marker="o") |
| |
| for i, edge in enumerate(edges): |
| x1, y1, z1 = vertices[edge[0]] |
| x2, y2, z2 = vertices[edge[1]] |
| ax.plot([x1, x2], [y1, y2], [z1, z2], c="k") |
| |
| ax.set_xlim([-1, 2]) |
| ax.set_ylim([-1, 2]) |
| ax.set_zlim([-1, 2]) |
| |
| plt.show() |
效果图

【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性