要解决“ax.plot_surface覆盖了后续的ax.scatter”的问题,可以使用zorder
参数来控制绘图顺序。较高的zorder
值将使图形元素位于较低值的元素之上。
以下是一个示例代码:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 绘制曲面
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
ax.plot_surface(X, Y, Z, cmap='viridis', zorder=1)
# 绘制散点图
x_points = np.random.uniform(-5, 5, 100)
y_points = np.random.uniform(-5, 5, 100)
z_points = np.sin(np.sqrt(x_points**2 + y_points**2))
ax.scatter(x_points, y_points, z_points, color='red', zorder=2)
plt.show()
在此示例中,我们首先绘制了曲面图,然后绘制了散点图。通过将zorder
设置为较高的值(2),我们确保散点图位于曲面图之上。