import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 创建一个3D坐标轴
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 圆柱体的高度和半径
height = 5
radius = 1
# 创建圆柱体的表面网格
theta = np.linspace(0, 2. * np.pi, 50)
z = np.linspace(0, height, 50)
theta_grid, z_grid = np.meshgrid(theta, z)
x_grid = radius * np.cos(theta_grid)
y_grid = radius * np.sin(theta_grid)
# 绘制圆柱体的侧面
ax.plot_surface(x_grid, y_grid, z_grid, color='b', alpha=0.8)
# 绘制圆柱体的顶部和底部
ax.plot_surface(x_grid, y_grid, np.full_like(z_grid, 0), color='b', alpha=0.5) # 底部
ax.plot_surface(x_grid, y_grid, np.full_like(z_grid, height), color='b', alpha=0.5) # 顶部
# 设置坐标轴的范围和标签
ax.set_xlim([-1.5 * radius, 1.5 * radius])
ax.set_ylim([-1.5 * radius, 1.5 * radius])
ax.set_zlim([0, height + 1])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# 显示图形
plt.show()
程序结果: