你可以这样做:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [15.00, 6.0]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
r = 2
u, v = np.mgrid[0:2.01 * np.pi:(1/10)* np.pi, 0:1.01*np.pi:(1/10)* np.pi]
X = r * np.cos(u) * np.sin(v)
Y = r * np.sin(u) * np.sin(v)
Z = r * np.cos(v)
ax.plot_surface(X, Y, Z, cmap=plt.cm.YlGnBu_r)
plt.show()
返回:
更新
另一种方式,如果你想引入你想要的点(在这里,我随机化,但如果你想使用代码不变,你需要以完全相同的方式格式化你的点),你可以这样做:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
def random_point(r=2):
ct = 2*np.random.rand() - 1
st = np.sqrt( 1 - ct**2 )
phi = 2* np.pi * np.random.rand()
x = r * st * np.cos( phi)
y = r * st * np.sin( phi)
z = r * ct
return np.array( [x, y, z ] )
def near( p, pntList, d0 ):
cnt=0
for pj in pntList:
dist=np.linalg.norm( p - pj )
if dist < d0:
cnt += 1 - dist/d0
return cnt
Azimuth_points = np.array([ random_point(2.02) for i in range(23) ] )
fig = plt.figure()
ax = fig.add_subplot( 1, 1, 1, projection='3d')
u = np.linspace( 0, 2 * np.pi, 120)
v = np.linspace( 0, np.pi, 60 )
r = 2
X = r * np.outer( np.cos( u ), np.sin( v ) )
Y = r * np.outer( np.sin( u ), np.sin( v ) )
Z = r * np.outer( np.ones( np.size( u ) ), np.cos( v ) )
W = X.copy()
for i in range( len(X) ):
for j in range( len(X[0]) ):
x = X[ i, j ]
y = Y[ i, j ]
z = Z[ i, j ]
W[ i, j ] = near(np.array( [x, y, z ] ), Azimuth_points, 3)
W = W / np.amax( W )
myheatmap = W
ax.plot_surface( X, Y, Z, cstride=1, rstride=1, facecolors=cm.jet( myheatmap ) )
plt.show()
这使: