I am playing around with 3D graphs, and I created a couple of spheres. How do I make it so that the highest sphere is dark blue and the bottom sphere is white (with the other spheres in between varying in color according to the cmap)?
我正在玩3D图形,我创建了几个球体。如何使最高的球体为深蓝色,而最低的球体为白色(中间的其他球体的颜色根据Cmap而变化)?
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(projection='3d')
theta = np.linspace(0,np.pi,12)
phi = np.linspace(0,2*np.pi,12)
u, v = np.meshgrid(theta,phi)
x = []
y = []
z = []
for i in range(5):
x.append(np.cos(v) * np.sin(u) + i)
y.append(np.sin(u) * np.sin(v) + i)
z.append(np.cos(u) + i)
ax.plot_surface(x[i], y[i], z[i], cmap='Blues')
ax.set(xlabel = 'x', ylabel = 'y', zlabel = 'z', xlim = (-10,10), ylim = (-10,10), zlim = (-10,10))
ax.set_aspect('equal')
plt.show()
I've tried putting ax.plot_surface(x, y, z, cmap='Blues')
after the for loop, but then I always get the error: 'list' object has no attribute 'ndim'
.
我试着在for循环后加上ax.ploySurface(x,y,z,cmap=‘Blues’),但总是收到错误:‘list’对象没有‘ndim’属性。
My closest attempt has been this:
我最近的尝试是这样的:
for i in range(5):
x.append(np.cos(v) * np.sin(u) + i)
y.append(np.sin(u) * np.sin(v) + i)
z.append(np.cos(u) + i)
X = np.array(x)
Y = np.array(y)
Z = np.array(z)
ax.plot_trisurf(X.flatten(), Y.flatten(), Z.flatten(), cmap='Blues')
However, the spheres look all wonky
然而,球体看起来都摇摇晃晃的。
Help appreciated
感谢您的帮助
更多回答
Details (screenshot) of how the 'he spheres look all wonky' might help others determine your issue.
球体看起来摇摇晃晃的细节(屏幕截图)可能会帮助其他人确定你的问题。
我是一名优秀的程序员,十分优秀!