📜  用Python显示 3D 图像

📅  最后修改于: 2022-05-13 01:55:51.243000             🧑  作者: Mango

用Python显示 3D 图像

在本文中,我们将讨论如何在Python使用不同的方法(即 3d 投影、view_init() 方法和使用循环)来显示 3D 图像。

需要的模块

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

示例 1:

在这个例子中,我们创建了一个散射正弦波的 3d 图像。在这里,我们使用 'np.arrange' 和 'np.sin'.NumPy.sin 创建了一个点数组:这个数学函数帮助用户计算所有 x(作为数组元素)的三角正弦,另一个函数是scatter() 方法是用于绘制散点图的 matplotlib 库。

Python3
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
 
# Change the Size of Graph using
# Figsize
fig = plt.figure(figsize=(10, 10))
 
# Generating a 3D sine wave
ax = plt.axes(projection='3d')
 
# Creating array points using
# numpy
x = np.arange(0, 20, 0.1)
y = np.sin(x)
z = y*np.sin(x)
c = x + y
 
# To create a scatter graph
ax.scatter(x, y, z, c=c)
 
# trun off/on axis
plt.axis('off')
 
# show the graph
plt.show()


Python3
# Import libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
 
# Change the Size of Graph using
# Figsize
fig = plt.figure(figsize=(10, 10))
 
# Generating a 3D sine wave
ax = plt.axes(projection='3d')
 
# Create axis
axes = [5, 5, 5]
 
# Create Data
data = np.ones(axes)
 
# 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
 
# trun off/on axis
plt.axis('off')
 
# Voxels is used to customizations of
# the sizes, positions and colors.
ax.voxels(data, facecolors=colors, edgecolors='grey')


Python3
#Import libraries
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
 
#Change the Size of Graph using Figsize
fig = plt.figure(figsize=(10,10))
 
#Generating a 3D sine wave
ax = plt.axes(projection='3d')
 
 
# assigning coordinates
x = np.linspace(-1, 5, 10)
y = np.linspace(-1, 5, 10)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X ** 2 + Y ** 2))
 
# creating the visualization
ax.plot_wireframe(X, Y, Z, color ='green')
 
# trun off/on axis
plt.axis('off')


Python3
from numpy import linspace
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
 
 
# Creating 3D figure
fig = plt.figure(figsize=(8, 8))
ax = plt.axes(projection='3d')
 
# Creating Dataset
z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x, y, z, 'green')
 
# 360 Degree view
for angle in range(0, 360):
    ax.view_init(angle, 30)
    plt.draw()
    plt.pause(.001)
 
plt.show()


输出:

示例 2:

在这个例子中,我们选择了维度 X =5、Y=5、Z=5 的 3D 轴,并且在 np.ones() 中我们传递了立方体的维度。 np.ones()函数返回一个给定形状和类型的新数组,其中包含一个。

在上述步骤之后,我们选择颜色不透明度为 alpha = 0.9(从 0.0 – 1.0 变化)。在下一步中,我们在 np.empty()函数中传递轴的维度(即 5, 5, 5)+ 立方体的面数(即 0-4 ),然后我们传递颜色组合和不透明度立方体的每个面和最后的体素都用于自定义大小、位置和颜色。 np.empty()函数返回给定形状和类型的新数组,无需初始化条目。

蟒蛇3

# Import libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
 
# Change the Size of Graph using
# Figsize
fig = plt.figure(figsize=(10, 10))
 
# Generating a 3D sine wave
ax = plt.axes(projection='3d')
 
# Create axis
axes = [5, 5, 5]
 
# Create Data
data = np.ones(axes)
 
# 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
 
# trun off/on axis
plt.axis('off')
 
# Voxels is used to customizations of
# the sizes, positions and colors.
ax.voxels(data, facecolors=colors, edgecolors='grey')

输出:

示例 3:

在这个例子中,我们使用 numpy.linspace() 创建一个包含 10 个在 -1 和 5 之间线性放置的元素的数组,在网格函数返回两个二维数组之后,两者都包括在内,之后为了可视化图像3D 线框我们需要传递 X、Y、Z、颜色(可选)的坐标。

蟒蛇3

#Import libraries
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
 
#Change the Size of Graph using Figsize
fig = plt.figure(figsize=(10,10))
 
#Generating a 3D sine wave
ax = plt.axes(projection='3d')
 
 
# assigning coordinates
x = np.linspace(-1, 5, 10)
y = np.linspace(-1, 5, 10)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X ** 2 + Y ** 2))
 
# creating the visualization
ax.plot_wireframe(X, Y, Z, color ='green')
 
# trun off/on axis
plt.axis('off')

输出:

示例 4:

在此示例中,我们绘制了一个螺旋图,我们将使用循环查看其 360 度视图。这里, view_init(elev=, azim=)这可用于以编程方式旋转轴。'elev ' 存储 z 平面中的仰角。 'azim' 将方位角存储在 x,y plane.D 构造函数中。 matplotlib 库的 pyplot 模块中的 draw()函数用于以 0.001 时间间隔的暂停重新绘制当前图形。

蟒蛇3

from numpy import linspace
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
 
 
# Creating 3D figure
fig = plt.figure(figsize=(8, 8))
ax = plt.axes(projection='3d')
 
# Creating Dataset
z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x, y, z, 'green')
 
# 360 Degree view
for angle in range(0, 360):
    ax.view_init(angle, 30)
    plt.draw()
    plt.pause(.001)
 
plt.show()

输出: