x,y,自己设定步长,产生三维网格X,Y,并在一个图形窗口的四个子窗口分别绘制的三维网格图,带底座的三维网格图,三维曲面图和带等高线的三维曲面图。
时间: 2023-10-07 18:05:01 浏览: 66
以下是使用Python的Matplotlib库实现的代码:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 设置步长和范围
step = 0.1
x_range = np.arange(-2, 2 + step, step)
y_range = np.arange(-2, 2 + step, step)
z_range = np.arange(-2, 2 + step, step)
# 生成三维网格
X, Y, Z = np.meshgrid(x_range, y_range, z_range)
# 定义网格图绘制函数
def plot_grid(ax):
ax.plot_wireframe(X, Y, Z, color='gray')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# 定义底座函数
def base_function(x, y):
return np.sqrt(x**2 + y**2)
# 生成底座数据
base_X, base_Y = np.meshgrid(x_range, y_range)
base_Z = base_function(base_X, base_Y)
# 定义底座绘制函数
def plot_base(ax):
ax.plot_surface(base_X, base_Y, base_Z, cmap='coolwarm')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# 定义曲面函数
def surface_function(x, y):
return np.sin(np.sqrt(x**2 + y**2))
# 生成曲面数据
surface_Z = surface_function(X, Y)
# 定义曲面绘制函数
def plot_surface(ax):
ax.plot_surface(X, Y, surface_Z, cmap='coolwarm')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# 定义等高线绘制函数
def plot_contour(ax):
ax.contour(X, Y, surface_Z, cmap='coolwarm')
ax.set_xlabel('X')
ax.set_ylabel('Y')
# 创建图形窗口和四个子窗口
fig = plt.figure(figsize=(12, 8))
ax1 = fig.add_subplot(2, 2, 1, projection='3d')
ax2 = fig.add_subplot(2, 2, 2, projection='3d')
ax3 = fig.add_subplot(2, 2, 3, projection='3d')
ax4 = fig.add_subplot(2, 2, 4)
# 绘制三维网格图
plot_grid(ax1)
ax1.set_title('3D Grid')
# 绘制带底座的三维网格图
plot_grid(ax2)
plot_base(ax2)
ax2.set_title('3D Grid with Base')
# 绘制三维曲面图
plot_surface(ax3)
ax3.set_title('3D Surface')
# 绘制带等高线的三维曲面图
plot_contour(ax4)
plot_base(ax4)
ax4.set_title('3D Contour with Base')
# 调整子窗口间距和整个窗口的布局
plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95, wspace=0.2, hspace=0.3)
# 显示图形窗口
plt.show()
```
运行代码后,将会弹出一个图形窗口,其中包含四个子窗口,分别绘制了三维网格图、带底座的三维网格图、三维曲面图和带等高线的三维曲面图。你可以通过旋转和缩放子窗口来查看不同视角下的图形。