【中秋节快乐】Matplotlib:3d绘图合集

目录

一、环境介绍

二、Matplotlib绘图(3d)

 0. 设置中文字体

1. 3D线框图(3D Wireframe Plot)

2. 3D散点图(3D Scatter Plot)

 3. 3D条形图(3D Bar Plot)

4. 3D曲面图(3D Surface Plot)

 5. 3D等高线图(3D Contour Plot)

 6. 3D向量场图(3D Vector Field Plot)

7. 未完待续


一、环境介绍

matplotlib 3.5.3
numpy 1.21.6
python 3.7.16
  • 运行下述命令检查Python版本
 python --version 
  • 运行下述代码检查Python、NumPy、Matplotlib版本
import sys
import numpy as np
import matplotlib

print("Python 版本:", sys.version)
print("NumPy 版本:", np.__version__)
print("matplotlib 版本:", matplotlib.__version__)

二、Matplotlib绘图(3d)

 0. 设置中文字体

import matplotlib

matplotlib.rcParams['font.family'] = 'Microsoft YaHei'  # 设置为微软雅黑字体
matplotlib.rcParams['font.sans-serif'] = ['SimHei']     # 设置中文字体为黑体

        若不进行该设置,会报错字体缺失

1. 3D线框图(3D Wireframe Plot)

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

# 生成数据
x = np.linspace(-5, 5, 50)  # x轴坐标
y = np.linspace(-5, 5, 50)  # y轴坐标
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))  # z轴坐标,这里使用sin函数生成一个曲面

# 创建一个三维坐标系
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 绘制线框图
ax.plot_wireframe(X, Y, Z)

# 设置坐标轴标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# 显示图形
plt.show()

2. 3D散点图(3D Scatter Plot)

import matplotlib.pyplot as plt
import numpy as np

# 数据准备
x = np.random.rand(100)  # x轴数据
y = np.random.rand(100)  # y轴数据
z = np.random.rand(100)  # z轴数据
colors = np.random.rand(100)  # 颜色数据

# 创建3D图形对象
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 绘制3D散点图
ax.scatter(x, y, z, c=colors, cmap='viridis', marker='o')

# 设置坐标轴标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# 显示图形
plt.show()

 3. 3D条形图(3D Bar Plot)

import matplotlib.pyplot as plt
import numpy as np

# 数据准备
x = np.arange(3)  # x轴位置
y = np.arange(3)  # y轴位置
x_mesh, y_mesh = np.meshgrid(x, y)  # 创建网格
z = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])  # 条形的高度

# 创建3D图形对象
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 绘制3D条形图
ax.bar3d(x_mesh.flatten(), y_mesh.flatten(), np.zeros_like(z).flatten(), 0.5, 0.5, z.flatten())

# 设置坐标轴标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# 显示图形
plt.show()

   

4. 3D曲面图(3D Surface Plot)

import matplotlib.pyplot as plt
import numpy as np

# 数据准备
x = np.linspace(-5, 5, 100)  # x轴数据范围
y = np.linspace(-5, 5, 100)  # y轴数据范围
x_mesh, y_mesh = np.meshgrid(x, y)  # 创建网格
z = np.sin(np.sqrt(x_mesh**2 + y_mesh**2))  # 曲面高度

# 创建3D图形对象
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 绘制3D曲面图
ax.plot_surface(x_mesh, y_mesh, z, cmap='viridis')

# 设置坐标轴标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# 显示图形
plt.show()

  

 5. 3D等高线图(3D Contour Plot)

import matplotlib.pyplot as plt
import numpy as np

# 数据准备
x = np.linspace(-5, 5, 100)  # x轴数据范围
y = np.linspace(-5, 5, 100)  # y轴数据范围
x_mesh, y_mesh = np.meshgrid(x, y)  # 创建网格
z = np.sin(np.sqrt(x_mesh**2 + y_mesh**2))  # 曲面高度

# 创建3D图形对象
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 绘制3D等高线图
ax.contour3D(x_mesh, y_mesh, z, 50, cmap='viridis')

# 设置坐标轴标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# 显示图形
plt.show()

 6. 3D向量场图(3D Vector Field Plot)

import matplotlib.pyplot as plt
import numpy as np

# 数据准备
x = np.linspace(-2, 2, 10)  # x轴数据范围
y = np.linspace(-2, 2, 10)  # y轴数据范围
z = np.linspace(-2, 2, 10)  # z轴数据范围
x_mesh, y_mesh, z_mesh = np.meshgrid(x, y, z)  # 创建网格
u = np.sin(x_mesh) * np.cos(y_mesh) * np.cos(z_mesh)  # x方向分量
v = -np.cos(x_mesh) * np.sin(y_mesh) * np.cos(z_mesh)  # y方向分量
w = np.sqrt(2.0 / 3.0) * np.cos(x_mesh) * np.cos(y_mesh) * np.sin(z_mesh)  # z方向分量

# 创建3D图形对象
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 绘制3D向量场图
ax.quiver(x_mesh, y_mesh, z_mesh, u, v, w)

# 设置坐标轴标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# 显示图形
plt.show()

7. 未完待续

你可能感兴趣的:(#,Matplotlib,matplotlib,3d)