matplotlib中的球坐标图 [英] Spherical coordinates plot in matplotlib
问题描述
R(teta,phi)= cos(phi ^ 2),teta [0,2 * pi],phi [0,pi]
R(teta, phi) = cos(phi^2), teta[0, 2*pi], phi[0,pi]
如何借助matplotlib在球形坐标中绘制此函数(R(teta,phi))的图形? 我还没有找到球形坐标的文档.
How to draw a graph of this function (R(teta, phi)) in spherical coordinates with the help of matplotlib? The documentation I have not found Spherical coordinates.
推荐答案
The code below is very much like the 3D polar plot from the Matplotlib gallery. The only difference is that you use np.meshgrid
to make 2D arrays for PHI
and THETA
instead of R
and THETA
(or what the 3D polar plot example calls P
).
这个故事的寓意是,只要X
,Y
和Z
可以表示为两个参数的(平滑)函数,plot_surface
可以对其进行绘制.
The moral of the story is that as long as X
, Y
, and Z
can be expressed as (smooth) functions of two parameters, plot_surface
can plot it.
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d
theta, phi = np.linspace(0, 2 * np.pi, 40), np.linspace(0, np.pi, 40)
THETA, PHI = np.meshgrid(theta, phi)
R = np.cos(PHI**2)
X = R * np.sin(PHI) * np.cos(THETA)
Y = R * np.sin(PHI) * np.sin(THETA)
Z = R * np.cos(PHI)
fig = plt.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
plot = ax.plot_surface(
X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('jet'),
linewidth=0, antialiased=False, alpha=0.5)
plt.show()
收益
通常,半径R
应该为正,因此您可能需要
Typically R
, the radius, should be positive, so you might want
R = np.abs(np.cos(PHI**2))
在这种情况下,
import matplotlib.colors as mcolors
cmap = plt.get_cmap('jet')
norm = mcolors.Normalize(vmin=Z.min(), vmax=Z.max())
plot = ax.plot_surface(
X, Y, Z, rstride=1, cstride=1,
facecolors=cmap(norm(Z)),
linewidth=0, antialiased=False, alpha=0.5)
收益
谁知道R = np.abs(np.cos(PHI**2))
是穿裙子的小女孩? :)
Who knew R = np.abs(np.cos(PHI**2))
is a little girl in a dress? :)
这篇关于matplotlib中的球坐标图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!