如何在Python使用 Matplotlib 绘制 3D 立方体?
在本文中,我们将使用 matplotlib 和 Numpy 处理立方体的 3d 图。立方体是最基本的 3D 形状之一。立方体是由 6 个相同的正方形面包围的 3 维立体对象。立方体有 6 个面、12 个边和 8 个角。所有面都是相同大小的正方形。一个立方体的总表面积是6个相同正方形的面积之和。
Matplotlib 带有各种各样的图。图表有助于了解趋势、模式以建立相关性。 Matplotlib 被引入用于二维绘图。通过导入标准 Matplotlib 附带的 mplot3d 工具包启用 3d 绘图。导入后,可以通过将关键字投影 =“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()函数中传递轴的维度(即 5, 5, 5)+ 立方体的面数(即 0-4 ),然后我们传递颜色组合和不透明度立方体的每个面。
# 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 维(即投影 = '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)
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')
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)
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)
输出:
示例 2:
带网格和不同颜色的立方体
蟒蛇3
# 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。
Syntax: view_init(elev, azim)
Parameters:
- ‘elev’ stores the elevation angle in the z plane.
- ‘azim’ stores the azimuth angle in the x,y plane.
蟒蛇3
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。
蟒蛇3
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)
输出: