matplotlib.pyplot.plot_surface

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
demo07_3dsurface.py 3d 曲面图
"""
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import numpy as np

# 整理数据
n = 500
x, y = np.meshgrid(np.linspace(-3, 3, n),
np.linspace(-3, 3, n))
# print(x, y)
# 计算每个坐标点的高度
z = (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y**2)

# 绘制
plt.figure('3D surface', facecolor='lightgray')
plt.title('3D Surface', fontsize=18)
ax3d = plt.gca(projection='3d')
ax3d.set_xlabel('x', fontsize=14)
ax3d.set_ylabel('y', fontsize=14)
ax3d.set_zlabel('z', fontsize=14)
ax3d.plot_surface(x, y, z, cmap='jet',
rstride=30, # 行跨距
cstride=30) # 列跨距
plt.tick_params(labelsize=10)

# set unused spines invisible to fix error of tight_layout
for spine in ax3d.spines.values():
spine.set_visible(False)

plt.tight_layout()
plt.show()

3d_surface.png