问题可能在于对Axes3D对象的使用方式。散点图需要使用matplotlib.mplot3d库中的scatter函数,并且必须传递X,Y和Z坐标的三个数组。以下是示例代码,其中for循环迭代X,Y和Z数组中的数据点,每次迭代添加到Axes3D图中。
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X = np.random.normal(size=100)
Y = np.random.normal(size=100)
Z = np.random.normal(size=100)
for x, y, z in zip(X, Y, Z):
ax.scatter(x, y, z, c='blue', alpha=0.5)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()