如何在 Python 中使用 Matplotlib 绘制 3D 立方体? 您所在的位置:网站首页 卡车3D立体画 如何在 Python 中使用 Matplotlib 绘制 3D 立方体?

如何在 Python 中使用 Matplotlib 绘制 3D 立方体?

2024-07-12 16:15| 来源: 网络整理| 查看: 265

在本文中,我们将使用 matplotlib 和 Numpy 处理立方体的 3d 图。立方体是最基本的 3D 形状之一。立方体是由 6 个相同的正方形面包围的 3 维实体对象。立方体有 6 个面、12 个边和 8 个角。所有面都是相同大小的正方形。立方体的总表面积是6个相同正方形的面积之和。

Matplotlib 带有各种各样的图。图表有助于理解趋势、模式以建立关联。 Matplotlib 被引入用于二维绘图。通过导入标准 Matplotlib 附带的 mplot3d 工具包启用 3d 绘图。导入后,可以通过将关键字 projection=”3d” 传递给 Matplotlib 中的任何常规轴创建函数来创建 3D 图。

需要的模块 Matplotlib:它是一个用于 Python 编程的绘图库,用作可视化实用程序库,Matplotlib 基于 NumPy 数组构建,旨在与更广泛的 SciPy 堆栈配合使用。 Numpy:它是一个通用的数组处理包。它提供了一个高性能的多维数组和矩阵以及大量高级数学函数。 mpl_toolkits:它提供了一些基本的 3d 绘图(散点、冲浪、线、网格)工具。它是一个帮助类的集合,用于在 Matplotlib 中显示 3d 轴。 方法

第 1 步:导入库。

import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np

步骤 2:在这一步中,我们选择维度 X =5、Y=5、Z=5 的 3D 轴,并在 np.ones () 我们正在传递立方体的尺寸。

# Create axis axes = [5, 5, 5]

# Create Data data = np.ones(axes)

第 3 步:在这一步中,我们选择颜色不透明度为 alpha = 0.9(从 0.0 到 1.0 不等)。在下一步中,我们在 np. empty() 函数之后,我们为立方体的每个面传递颜色组合和不透明度。

# controll Tranperency alpha = 0.9

# control colour colors = np.empty(axes + [4])

colors[0] = [1, 0, 0, alpha] # red colors[1] = [0, 1, 0, alpha] # green colors[2] = [0, 0, 1, alpha] # blue colors[3] = [1, 1, 0, alpha] # yellow colors[4] = [1, 1, 1, alpha] # grey

步骤 4:在这一步中,我们使用 matplotlib 库的 figure() 函数来创建一个新图形,之后,我们使用 add_subplot() 方法将轴添加到图形中作为 3-Dimensional(即 Projection = '3d') 子图排列的一部分。它有 3 个参数。

网格中的行数, 网格中的列数,以及, 必须放置新子图的位置。

需要注意的是 fig.add_subplot(1, 1, 1) 等价于 fig.add_subplot(111)。

# Plot figure fig = plt.figure() ax = fig.add_subplot(111, projection='3d')

第 5 步:在最后一步中,体素用于自定义大小、位置和网格颜色。上面提供了正确的语法。

# Voxels is used to customizations of the # sizes, positions and colors. ax.voxels(data, facecolors=colors, edgecolors='grey')

示例 1:一种颜色的简单立方体。

这里改变颜色[:],意味着我们将所有数组元素选择为一种颜色(即红色),并且为了删除网格,我们从体素方法中删除了“edgecolor”参数,以仅具有一种颜色立方体。

Python3实现

# Import libraries import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np     # Create axis axes = [5, 5, 5]   # Create Data data = np.ones(axes, dtype=np.bool)   # Controll Tranperency alpha = 0.9   # Control colour colors = np.empty(axes + [4], dtype=np.float32)   colors[:] = [1, 0, 0, alpha]  # red   # Plot figure fig = plt.figure() ax = fig.add_subplot(111, projection='3d')   # Voxels is used to customizations of the # sizes, positions and colors. ax.voxels(data, facecolors=colors)

输出:

示例 2:

带有网格和不同颜色的立方体

Python3实现

# Import libraries import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np     # Create axis axes = [5, 5, 5]   # Create Data data = np.ones(axes, dtype=np.bool)   # Controll Tranperency alpha = 0.9   # Control colour colors = np.empty(axes + [4], dtype=np.float32)   colors[0] = [1, 0, 0, alpha]  # red colors[1] = [0, 1, 0, alpha]  # green colors[2] = [0, 0, 1, alpha]  # blue colors[3] = [1, 1, 0, alpha]  # yellow colors[4] = [1, 1, 1, alpha]  # grey   # Plot figure fig = plt.figure() ax = fig.add_subplot(111, projection='3d')   # Voxels is used to customizations of # the sizes, positions and colors. ax.voxels(data, facecolors=colors, edgecolors='grey')

输出:

示例 3:沿 Y 轴的面立方体

在本例中,我们将再添加一行代码 view_init( ) 以根据需要更改轴视图。 view_init() 可用于以编程方式更改轴视图。这里我们使用 elev=100 和 azim=0。

语法:view_init(elev, azim)

参数:

'elev' 存储 z 平面中的仰角。 ‘azim’ 存储 x,y 平面中的方位角。 Python3实现

import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np   axes = [5, 5, 5] data = np.ones(axes, dtype=np.bool)   colors = np.empty(axes + [4], dtype=np.float32)   # Controll Tranperency alpha = .7   # Control colors colors[0] = [1, 0, 0, alpha] colors[1] = [0, 1, 0, alpha] colors[2] = [0, 0, 1, alpha] colors[3] = [1, 1, 0, alpha] colors[4] = [0, 1, 1, alpha]   # set all internal colors to  # black with alpha=1 colors[1:-1, 1:-1, 1:-1, 0:3] = 0 colors[1:-1, 1:-1, 1:-1, 3] = 1   # Plot figure fig = plt.figure() ax = fig.add_subplot(111, projection='3d')   # Control number of slice data[-1] = True data[-2] = False data[-3] = False data[-4] = False data[-5] = True   # Voxels is used to customizations of  # the sizes, positions and colors. ax.voxels(data, facecolors=colors, edgecolors='pink')   # it can be used to change the axes view ax.view_init(100, 0)

输出:

示例 4:沿 X 轴的面立方体

view_init() 可用于以编程方式更改轴视图。这里我们使用 elev=100 和 azim=90。

Python3实现

import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np   axes = [5, 5, 5] data = np.ones(axes, dtype=np.bool)   colors = np.empty(axes + [4], dtype=np.float32)   # Controll Tranperency alpha = .7   # Control colors colors[0] = [1, 0, 0, alpha] colors[1] = [0, 1, 0, alpha] colors[2] = [0, 0, 1, alpha] colors[3] = [1, 1, 0, alpha] colors[4] = [0, 1, 1, alpha]   # set all internal colors to  # black with alpha=1 colors[1:-1, 1:-1, 1:-1, 0:3] = 0 colors[1:-1, 1:-1, 1:-1, 3] = 1   # Plot figure fig = plt.figure() ax = fig.add_subplot(111, projection='3d')   # Control number of slice data[-1] = 1 data[-2] = False data[-3] = False data[-4] = False data[-5] = True   # Voxels is used to customizations  # of the sizes, positions and colors. ax.voxels(data, facecolors=colors, edgecolors='pink')   # it can be used to change the axes view ax.view_init(100, 90)

输出:



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有