我正在尝试使用 python 的 matplotlib 包在球坐标中绘制某个函数 Q(theta, phi)。我希望颜色显示为与原点的距离而不是 z 坐标的函数。有没有办法做到这一点?这是我用来生成 3D 曲面图的代码:
%matplotlib notebook
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(111, projection='3d')
X, Y = Q*np.sin(Theta)*np.cos(Phi), Q*np.sin(Theta)*np.sin(Phi)
Z = Q*np.cos(Theta)
ax.plot_surface(X, Y, Z, cmap=plt.cm.YlGnBu_r)
ax.set_zlim(-1, 1)
ax.set_ylim(-1, 1)
ax.set_xlim(-1, 1)
ax.set_xlabel('J_x')
ax.set_ylabel('J_y')
ax.set_zlabel('J_z')
plt.show()
我没有赢得足够的声誉来发布我创建的情节。它类似于此链接中的图:https ://matplotlib.org/3.1.1/gallery/mplot3d/surface3d.html 。如您所见,颜色是 z 坐标的函数,而不是与原点的距离的函数。
