说明:
# 设置轴比例相等,以获得圆柱体视角
ax.set_aspect('equal')
代码案例:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# 创建数据
theta = np.linspace(0, 2*np.pi, 100)
z = np.linspace(0, 1, 100)
r = z**2 * np.sin(theta) / (2*np.pi)
x = r * np.cos(theta)
y = r * np.sin(theta)
# 创建3D图形并绘制圆柱体
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, rstride=5, cmap='viridis')
# 设置轴比例相等,以获得圆柱体视角
ax.set_aspect('equal')
# 设置轴标签和标题
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Cylinder Plot')
plt.show()
图形比对:
不添加set_aspect图形
添加set_aspect图形