IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> Python画正方体 -> 正文阅读

[Python知识库]Python画正方体

画个立方体

matplotlib中,通过plt.voxels绘制立方体,下面先绘制一个最质朴的立方体

在这里插入图片描述

代码为

import matplotlib.pyplot as plt
import numpy as np

x, y, z = np.indices((2, 2, 2))
filled = np.ones((1,1,1))
ax = plt.subplot(projection='3d')
ax.voxels(x,y,z, filled=filled)
plt.show()

其中,x,y,z表示顶点,filled表示被填充的区域。由于其顶点数量为 2 × 2 × 2 2\times2\times2 2×2×2,故只有一个立方体,从而filled是一个 1 × 1 × 1 1\times1\times1 1×1×1的张量。

按照这个思路,可以做一个魔方

x, y, z = np.indices((4, 4, 4))
filled = np.ones((3,3,3))
cFace = np.where(filled, '#00AAAAA0', '#00AAAAA0')
cEdge = np.where(filled, '#008888', '#008888')
ax = plt.subplot(projection='3d')
ax.voxels(x,y,z, filled=filled, facecolors=cFace, edgecolors=cEdge)
plt.show()

其效果为

在这里插入图片描述

由于 x , y , z x,y,z x,y,z均为 4 × 4 × 4 4\times4\times4 4×4×4,则这些顶点共分割出 3 × 3 × 3 3\times3\times3 3×3×3的立方体,而filled填充,facecolors对面上色以及edgecolors对线上色,都是针对这 3 × 3 × 3 3\times3\times3 3×3×3的立方体而言的。其中,颜色的格式是RGBA,A就是透明度。

接下来,做一个八叉树的示意图

ax = plt.subplot(projection='3d')
ax.set_xlim(0,2)
ax.set_ylim(0,2)
ax.set_zlim(0,2)
ax.axis('off')

x, y, z = np.indices((3, 3, 3))
filled = np.ones((2,2,2))
filled[1,1,1] = False   #(1,1,1)这个立方体将要继续被分割
cFace = np.where(filled, '#00AAAACC', '#00AAAACC')
cEdge = np.where(filled, '#008888', '#008888')
ax.voxels(x,y,z, filled=filled, facecolors=cFace, edgecolors=cEdge) #第一次分割

x, y, z = np.indices((3, 3, 3))/2+1
cFace = np.where(filled, '#00AA00DD', '#00AA00DD')
cEdge = np.where(filled, '#008800', '#008800')
ax.voxels(x,y,z, filled=filled, facecolors=cFace, edgecolors=cEdge) #第二次分割

x, y, z = np.indices((3, 3, 3))/4+1.5
filled[1,1,1] = True
cFace = np.where(filled, '#0000AAEE', '#0000AAEE')
cEdge = np.where(filled, '#000000', '#000000')
ax.voxels(x,y,z, filled=filled, facecolors=cFace, edgecolors=cEdge) #第三次分割


plt.show()

其效果为

在这里插入图片描述

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-05-24 18:07:23  更:2022-05-24 18:08:59 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/22 1:14:13-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码