当前位置: 首页 > news >正文

基于python的三维装箱可视化

背景介绍

本文主要介绍两种基于python的三维装箱可视化能力,第一种是基于mpl_toolkits的静态三维可视化代码,另外一种是基于matplotlib的动态可视化代码。

mpl_toolkits实现

Axes3D简介

mpl_toolkits 是 matplotlib 库的一个模块集合,它包含了多个为 matplotlib 增加额外功能的工具包。这些工具包提供了扩展 matplotlib 标准功能的方法,允许用户绘制高级图表,以及为图表添加特别的特征和样式。

Axes3D 是 mpl_toolkits.mplot3d 模块中的一个类,用于创建和处理三维坐标轴。该类是 matplotlib 中绘制三维图形的核心,它扩展了 matplotlib 的二维绘图库,实现了三维空间中的可视化。与 matplotlib 二维坐标轴的 Axes 类似,Axes3D 提供一系列标准的三维绘图方法。

使用 Axes3D 可以方便地绘制三维散点图、线图、曲面图、线框图和三维条形图等。除了支持基本的三维绘图,Axes3D 还提供了工具来控制视图角度、缩放级别、以及坐标轴的刻度和标签。此外,Axes3D 支持交互性操作,如旋转和缩放,这些功能使得用户能以直观的方式探索三维数据。

代码实现
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3Ddef plot_3d_boxes(positions, sizes, container_size):fig = plt.figure(figsize=[40,10])ax = fig.add_subplot(111, projection='3d')# 根据盒子尺寸选择颜色colors = plt.cm.viridis(np.linspace(0, 1, len(sizes)))for i, (position, size) in enumerate(zip(positions, sizes)):color = colors[i]x, y, z = positiondx, dy, dz = sizeax.bar3d(x, y, z, dx, dy, dz, color=color)# 设置容器尺寸ax.set_box_aspect([container_size[0], container_size[1], container_size[2]])  # 设置显示的长宽高比例ax.set_xlim(0, container_size[0])ax.set_ylim(0, container_size[1])ax.set_zlim(0, container_size[2])# 设置轴标签ax.set_xlabel('X')ax.set_ylabel('Y')ax.set_zlabel('Z')ax.set_xlim([0, container_size[0]])ax.set_ylim([0, container_size[1]])ax.set_zlim([0, container_size[2]])plt.show()# 示例数据
positions = [[0, 0, 0], [3, 5, 0], [6, 8, 0]]
sizes = [[3, 3, 3], [3, 1, 2], [2, 3, 1]]
container_size = [10, 10, 10]# 绘制三维箱子图
plot_3d_boxes(positions, sizes, container_size)
结果展示

在这里插入图片描述

matplotlib实现

FuncAnimation简介

动画是基于FuncAnimation实现的, FuncAnimation是 matplotlib.animation 包中的一个类,它用于创建基于 matplotlib 的动画。该类通过在绘图元素上进行重复的更新和绘制操作,来生成一系列连续的图像帧,并将它们组合成为动画。使用 FuncAnimation,你可以创建帧序列动画,并定义每帧中发生的变化。这使得它适合于演示数据随时间变化的过程,或是根据一些参数的动态变化来展示数据。

代码实现
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.widgets import Button# 绘制单个箱子的函数
def draw_single_box(ax, position, size, color):x, y, z = positiondx, dy, dz = sizeax.bar3d(x, y, z, dx, dy, dz, color=color)# 动画更新函数
def update(frame, positions, sizes, colors, ax, container_size, anim_running):# 在更新时检查动画是否应该继续运行if anim_running[0]:ax.cla()  # 清除旧的帧ax.set_xlim(0, container_size[0])ax.set_ylim(0, container_size[1])ax.set_zlim(0, container_size[2])ax.set_xlabel('X')ax.set_ylabel('Y')ax.set_zlabel('Z')for i in range(frame + 1):draw_single_box(ax, positions[i], sizes[i], colors[i % len(colors)])# 暂停和播放的回调函数
def onClickPause(event, anim_running):anim_running[0] = Falsedef onClickPlay(event, anim_running):anim_running[0] = True# 函数执行绘制动画
def animate_boxes(positions, sizes, container_size):fig = plt.figure(figsize=(20, 10))ax = fig.add_subplot(111, projection='3d')colors = plt.cm.viridis(range(len(sizes)))# 初始化用于控制动画运行状态的列表anim_running = [True]# 设置按钮和事件pause_ax = plt.axes((0.81, 0.05, 0.1, 0.075))pause_button = Button(pause_ax, 'Pause')play_ax = plt.axes((0.7, 0.05, 0.1, 0.075))play_button = Button(play_ax, 'Play')# 绑定回调函数时把 anim_running 作为参数传递pause_button.on_clicked(lambda event: onClickPause(event, anim_running))play_button.on_clicked(lambda event: onClickPlay(event, anim_running))# 创建动画anim = FuncAnimation(fig, update, frames=len(sizes),fargs=(positions, sizes, colors, ax, container_size, anim_running),interval=400, repeat=True)plt.show()# 示例数据
positions = [[0, 22, 0], [0, 0, 0], [40, 37, 14]]
sizes = [[36, 36, 36], [59, 39, 20], [54, 40, 21]]
container_size = [100, 100, 100]  # 假设容器尺寸为 100x100x100# 运行动画
animate_boxes(positions, sizes, container_size)

结果展示

展示过程是循环播放的,可以通过点击pause暂停,点击play继续播放。
在这里插入图片描述

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • ArcGIS Pro SDK (四)框架 2
  • Harbor本地仓库搭建002_Harbor负载均衡节点搭建_nginx安装配置_harbor安装---分布式云原生部署架构搭建002
  • pdf怎么压缩到2m以内或5m以内的方法
  • 贪心算法——赶作业(C++)
  • 14、modbus poll 使用教程小记1
  • iOS18新增通话录音和应用锁!附升级教程及内置壁纸
  • Blender下使用python设置骨骼旋转
  • Java进阶示例
  • 【100个C++面试题和解答】
  • 电脑怎么录音?分享2种音频录制方法
  • iOS 18 Siri 升级之后都有哪些改变?
  • Idea连接GitLab的过程以及创建在gitlab中创建用户和群组
  • 【无标题】蓝桥杯软件赛Java研究生组/A组)第二章基础算法-第四节:构造
  • WDF驱动开发-硬件资源(一)
  • ElasticSearch学习篇13_《检索技术核心20讲》进阶篇之LSM树
  • 分享一款快速APP功能测试工具
  • [译]前端离线指南(上)
  • Android系统模拟器绘制实现概述
  • Invalidate和postInvalidate的区别
  • leetcode378. Kth Smallest Element in a Sorted Matrix
  • Netty+SpringBoot+FastDFS+Html5实现聊天App(六)
  • Python socket服务器端、客户端传送信息
  • Python利用正则抓取网页内容保存到本地
  • spring + angular 实现导出excel
  • VirtualBox 安装过程中出现 Running VMs found 错误的解决过程
  • VUE es6技巧写法(持续更新中~~~)
  • 表单中readonly的input等标签,禁止光标进入(focus)的几种方式
  • 电商搜索引擎的架构设计和性能优化
  • 对象引论
  • 基于HAProxy的高性能缓存服务器nuster
  • ------- 计算机网络基础
  • 利用jquery编写加法运算验证码
  • 体验javascript之美-第五课 匿名函数自执行和闭包是一回事儿吗?
  • 自动记录MySQL慢查询快照脚本
  • ​LeetCode解法汇总2696. 删除子串后的字符串最小长度
  • ​Z时代时尚SUV新宠:起亚赛图斯值不值得年轻人买?
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • #每日一题合集#牛客JZ23-JZ33
  • (11)MSP430F5529 定时器B
  • (ZT)北大教授朱青生给学生的一封信:大学,更是一个科学的保证
  • (笔试题)合法字符串
  • (七)理解angular中的module和injector,即依赖注入
  • (十)【Jmeter】线程(Threads(Users))之jp@gc - Stepping Thread Group (deprecated)
  • (一)使用Mybatis实现在student数据库中插入一个学生信息
  • (转)PlayerPrefs在Windows下存到哪里去了?
  • (自用)仿写程序
  • ... fatal error LINK1120:1个无法解析的外部命令 的解决办法
  • .cn根服务器被攻击之后
  • .form文件_SSM框架文件上传篇
  • .NET 4.0中的泛型协变和反变
  • .Net Core与存储过程(一)
  • .net framwork4.6操作MySQL报错Character set ‘utf8mb3‘ is not supported 解决方法
  • .NET 的程序集加载上下文
  • .NET 依赖注入和配置系统
  • .net 怎么循环得到数组里的值_关于js数组