前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用 Matplotlib 在 Python 中进行三维绘图

使用 Matplotlib 在 Python 中进行三维绘图

作者头像
用户1418987
发布2023-10-26 14:21:01
2.5K0
发布2023-10-26 14:21:01
举报
文章被收录于专栏:coder

使用 Matplotlib 在 Python 中进行三维绘图

3D 图是可视化具有三个维度的数据(例如具有两个因变量和一个自变量的数据)的非常重要的工具。通过在 3D 图中绘制数据,我们可以更深入地了解具有三个变量的数据。我们可以使用各种 matplotlib 库函数来绘制 3D 绘图。

使用 Matplotlib 进行三维绘图的示例

我们首先使用Matplotlib库绘制 3D 轴。为了绘制 3D 轴,我们只需将plt.axes()的投影参数从 None 更改为 3D。

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt


fig = plt.figure()
ax = plt.axes(projection='3d')

输出:

使用 Matplotlib 在 Python 中进行三维绘图_数据
使用 Matplotlib 在 Python 中进行三维绘图_数据

使用 matplotlib 绘制 3D 轴

使用上述语法,启用三维轴,并且可以在 3 个维度上绘制数据。3 维图提供了一种动态方法,使数据更具交互性。与 2-D 图一样,我们可以使用不同的方式来表示来绘制 3-D 图。我们可以制作散点图、等高线图、曲面图等。让我们看看不同的 3-D 图。 由线和点组成的图是最简单的 3 维图。我们将使用ax.plot3d 和ax.scatter函数分别绘制线图和点图。

使用 Matplotlib绘制 3 维线图

为了绘制 3 维线图,我们将使用 mpl_toolkits 库中的 mplot3d 函数。为了在 3D 中绘制直线,我们必须为直线方程初始化三个变量点。在我们的例子中,我们将定义三个变量x、y 和 z。  

代码语言:javascript
复制
# importing mplot3d toolkits, numpy and matplotlib
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()

# syntax for 3-D projection
ax = plt.axes(projection ='3d')

# defining all 3 axis
z = np.linspace(0, 1, 100)
x = z * np.sin(25 * z)
y = z * np.cos(25 * z)

# plotting
ax.plot3D(x, y, z, 'green')
ax.set_title('3D line plot geeks for geeks')
plt.show()

输出:

使用 Matplotlib 在 Python 中进行三维绘图_3D_02
使用 Matplotlib 在 Python 中进行三维绘图_3D_02

 使用 matplotlib 库绘制 3D 线图

使用 Matplotlib 绘制 3 维散点图

要使用散点绘制相同的图形,我们将使用matplotlib 中的scatter()函数。它将使用不同的点绘制相同的直线方程。 

代码语言:javascript
复制
# importing mplot3d toolkits
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()

# syntax for 3-D projection
ax = plt.axes(projection ='3d')

# defining axes
z = np.linspace(0, 1, 100)
x = z * np.sin(25 * z)
y = z * np.cos(25 * z)
c = x + y
ax.scatter(x, y, z, c = c)

# syntax for plotting
ax.set_title('3d Scatter plot geeks for geeks')
plt.show()

输出:

使用 Matplotlib 在 Python 中进行三维绘图_3d_03
使用 Matplotlib 在 Python 中进行三维绘图_3d_03

使用 Matplotlib 库绘制 3D 点图

使用 Matplotlib 库绘制曲面图  

曲面图和线框图适用于网格数据。他们获取网格值并将其绘制在三维表面上。我们将使用plot_surface()函数来绘制曲面图。

代码语言:javascript
复制
# importing libraries
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

# defining surface and axes
x = np.outer(np.linspace(-2, 2, 10), np.ones(10))
y = x.copy().T
z = np.cos(x ** 2 + y ** 3)

fig = plt.figure()

# syntax for 3-D plotting
ax = plt.axes(projection='3d')

# syntax for plotting
ax.plot_surface(x, y, z, cmap='viridis',\
				edgecolor='green')
ax.set_title('Surface plot geeks for geeks')
plt.show()

输出:

使用 Matplotlib 在 Python 中进行三维绘图_3d_04
使用 Matplotlib 在 Python 中进行三维绘图_3d_04

使用 matplotlib 库绘制曲面图

使用 Matplotlib 库绘制线框图  

为了绘制线框图,我们将使用matplotlib 库中的plot_wireframe()函数。

代码语言:javascript
复制
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt


# function for z axis
def f(x, y):
	return np.sin(np.sqrt(x ** 2 + y ** 2))

# x and y axis
x = np.linspace(-1, 5, 10)
y = np.linspace(-1, 5, 10)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)

fig = plt.figure()
ax = plt.axes(projection ='3d')
ax.plot_wireframe(X, Y, Z, color ='green')
ax.set_title('wireframe geeks for geeks');

输出:

使用 Matplotlib 在 Python 中进行三维绘图_3D_05
使用 Matplotlib 在 Python 中进行三维绘图_3D_05

使用 matplotlib 库的 3D 线框图

使用 Matplotlib 库绘制等高线图

等值线图采用二维规则网格中的所有输入数据,并在每个点评估 Z 数据。我们使用 ax.contour3D 函数来绘制等高线图。等高线图是可视化优化图的绝佳方法。 

代码语言:javascript
复制
def function(x, y):
	return np.sin(np.sqrt(x ** 2 + y ** 2))


x = np.linspace(-10, 10, 40)
y = np.linspace(-10, 10, 40)

X, Y = np.meshgrid(x, y)
Z = function(X, Y)

fig = plt.figure(figsize=(10, 8))
ax = plt.axes(projection='3d')

ax.plot_surface(X, Y, Z, cmap='cool', alpha=0.8)

ax.set_title('3D Contour Plot of function(x, y) =\
				sin(sqrt(x^2 + y^2))', fontsize=14)
ax.set_xlabel('x', fontsize=12)
ax.set_ylabel('y', fontsize=12)
ax.set_zlabel('z', fontsize=12)

plt.show()

输出:

使用 Matplotlib 在 Python 中进行三维绘图_数据_06
使用 Matplotlib 在 Python 中进行三维绘图_数据_06

使用 matplotlib 绘制函数的 3D 等高线图 

在 Python 中绘制曲面三角剖分 

上图有时过于受限且不方便。因此,通过这种方法,我们使用一组随机抽签。函数ax.plot_trisurf 用于绘制该图。虽然不是那么明确,但是更加灵活。

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.tri import Triangulation

def f(x, y):
	return np.sin(np.sqrt(x ** 2 + y ** 2))

x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)

tri = Triangulation(X.ravel(), Y.ravel())

fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

ax.plot_trisurf(tri, Z.ravel(), cmap='cool', edgecolor='none', alpha=0.8)

ax.set_title('Surface Triangulation Plot of f(x, y) =\
				sin(sqrt(x^2 + y^2))', fontsize=14)
ax.set_xlabel('x', fontsize=12)
ax.set_ylabel('y', fontsize=12)
ax.set_zlabel('z', fontsize=12)

plt.show()

输出:

使用 Matplotlib 在 Python 中进行三维绘图_数据_07
使用 Matplotlib 在 Python 中进行三维绘图_数据_07

使用 matplotlib 绘制等高线图的表面三角测量图 

在Python中绘制莫比乌斯带 

莫比乌斯带也称为扭曲圆柱体,是一种没有边界的单面表面。要创建莫比乌斯带,请考虑其参数化,它是一个二维带,我们需要两个内在维度。其围绕环的角度范围为 0 到 2 个扇形,宽度范围为 -1 到 1。

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Define the parameters of the Möbius strip
R = 2

# Define the Möbius strip surface
u = np.linspace(0, 2*np.pi, 100)
v = np.linspace(-1, 1, 100)
u, v = np.meshgrid(u, v)
x = (R + v*np.cos(u/2)) * np.cos(u)
y = (R + v*np.cos(u/2)) * np.sin(u)
z = v * np.sin(u/2)

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

# Plot the Möbius strip surface
ax.plot_surface(x, y, z, alpha=0.5)

# Set plot properties
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Möbius Strip')

# Set the limits of the plot
ax.set_xlim([-3, 3])
ax.set_ylim([-3, 3])
ax.set_zlim([-3, 3])

# Show the plot
plt.show()

输出:

使用 Matplotlib 在 Python 中进行三维绘图_3D_08
使用 Matplotlib 在 Python 中进行三维绘图_3D_08

使用 matplotlib 库的莫比乌斯带图 

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-10-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用 Matplotlib 在 Python 中进行三维绘图
  • 使用 Matplotlib 进行三维绘图的示例
    • 使用 Matplotlib绘制 3 维线图
      • 使用 Matplotlib 绘制 3 维散点图
        • 使用 Matplotlib 库绘制曲面图  
          • 使用 Matplotlib 库绘制线框图  
            • 使用 Matplotlib 库绘制等高线图
              • 在 Python 中绘制曲面三角剖分 
                • 在Python中绘制莫比乌斯带 
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档