【发布时间】:2016-01-19 03:10:45
【问题描述】:
我要绘制一个在极坐标中的函数。
首先我设置径向 (r) 和极坐标 (phi) 变量的值。
pi=np.pi
r=np.linspace(0,4,30)
phi=np.linspace(0,2*pi,10)
然后我做 A0(r,phi),其中 A0 是我编写的函数,它对 r 和 phi 的每个值都有一个实值。
我存储它的方式是 A0[r, phi]。巫婆的意思是“行”表示径向坐标,“列”表示极坐标。
Ao,如果我在 r 中使用 n 个值,在 phi 中使用 m 个值,则 A0 是 (n,m) Matrix 或 (n,m) 形状的 numpy 数组。 (在我的例子中,它是一个 (30,10) numpy 数组)
'A0' 似乎可以正常工作,但问题是我不知道如何绘制它。
我正在尝试类似的东西:
fig=pyplot.figure()
ax = pyplot.subplot(111, projection='3d')
R, P = np.meshgrid(r, phi)
X, Y = R*np.cos(P), R*np.sin(P)
ax.plot_wireframe(X,Y, A0, rstride=10, cstride=10)
尝试遵循 matplotlib 示例,但它说形状不一致,因为它的尺寸有问题(在 X、Y 和 A0 之间)。
有什么想法吗??
如果我这样做效果很好:
fig = pyplot.figure(figsize=(11,7), dpi=100)
ax = pyplot.subplot(111, projection='3d')
for j, rn in enumerate(r):
for k, phin in enumerate(phi):
ax.scatter(rn*np.cos(phin), rn*np.sin(phin), A0[j,k],',b')
但我想要的是曲面图或线图,而不是散点图。
以下是完整代码:
import numpy as np
from scipy.special import binom as binom
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot
from matplotlib import cm
import matplotlib
%matplotlib inline
'''Define A_(pm) (Gauss-laguerre polinomials) '''
def Ln(p,m,xj):
Ln=0.
for i in range(p+1):#el p+1 es por la indexacion
Ln=Ln+((-1)**i)*binom(p+m,p-i)*(xj**i)/np.math.factorial(i)
return Ln
def A(p, m, r, phi):#defino G-L
A0=np.zeros((len(r),len(phi)))
for j, rn in enumerate(r):
A0[j]=np.exp(-rn**2)*Ln(p,0,2*rn**2)
A1=np.zeros((len(r),len(phi)))
for j, rn in enumerate(r):
for k, phin in enumerate(phi):
A1[k,j]=2*((2*rn**2)**(m/2))*np.sqrt(np.math.factorial(p)/np.math.factorial(p+m))*np.exp(-rn**2)*Ln(p,m,2*(rn**2))*np.sin(m*phin)
A2=np.zeros((len(r),len(phi)))
for j, rn in enumerate(r):
for k, phin in enumerate(phi):
A2[k,j]=2*(2*rn**2)**(m/2)*np.sqrt(np.math.factorial(p)/np.math.factorial(p+m))*np.exp(-rn**2)*Ln(p,m,2*rn**2)*np.cos(m*phin)
return A0, A1, A2
pi=np.pi
r=np.linspace(0,6,50)
phi=np.linspace(0,2*pi,50)
A0, A1, A2=A(5,1,r,phi)#i think a have a bugs with the values of m .
fig = pyplot.figure(figsize=(11,7), dpi=100)
ax = pyplot.subplot(111, projection='3d')
for j, rn in enumerate(r):
for k, phin in enumerate(phi):
#ax.scatter(rn*np.cos(phin), rn*np.sin(phin), A0[j,k],'.b')
ax.scatter(rn*np.cos(phin), rn*np.sin(phin), A1[j,k],'.r')
#ax.scatter(rn*np.cos(phin), rn*np.sin(phin), A2[j,k],'.r')
这可行,但分散不是我想要的。 这是我尝试做的,但如果 r 和 phi 具有不同的维度,则它不起作用。
fig = pyplot.figure(figsize=(11,9), dpi=100)
ax = pyplot.subplot(111, projection='3d')
R, P = np.meshgrid(r, phi)
X, Y = R*np.cos(P), R*np.sin(P)
print 'shape r=', np.shape(r)
print 'shape R=', np.shape(R)
print 'shape P=', np.shape(P)
A0, A1, A2=A(5,0,R[1],R[0])
ax.plot_surface(X, Y, np.transpose(A0), rstride=3, cstride=3, cmap=pyplot.get_cmap('viridis'))
【问题讨论】:
标签: python python-2.7 math matplotlib plot