简单修复!
我设法使它在3.3.1版中工作。
看来此问题可能已在PR#17172中解决;您可以使用该ax.set_box_aspect([1,1,1])
函数来确保方面正确(请参见set_aspect函数的注释)。当与@karlo和/或@Matee Ulhaq提供的边界框函数一起使用时,这些图现在在3D中看起来是正确的!
最低工作实例
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d
import numpy as np
def set_axes_equal(ax: plt.Axes):
"""Set 3D plot axes to equal scale.
Make axes of 3D plot have equal scale so that spheres appear as
spheres and cubes as cubes. Required since `ax.axis('equal')`
and `ax.set_aspect('equal')` don't work on 3D.
"""
limits = np.array([
ax.get_xlim3d(),
ax.get_ylim3d(),
ax.get_zlim3d(),
])
origin = np.mean(limits, axis=1)
radius = 0.5 * np.max(np.abs(limits[:, 1] - limits[:, 0]))
_set_axes_radius(ax, origin, radius)
def _set_axes_radius(ax, origin, radius):
x, y, z = origin
ax.set_xlim3d([x - radius, x + radius])
ax.set_ylim3d([y - radius, y + radius])
ax.set_zlim3d([z - radius, z + radius])
u = np.linspace(0, 2*np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = np.outer(np.cos(u), np.sin(v))
y = np.outer(np.sin(u), np.sin(v))
z = np.outer(np.ones(np.size(u)), np.cos(v))
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(x, y, z)
ax.set_box_aspect([1,1,1])
set_axes_equal(ax)
plt.show()
equal
语句-它将始终相等。