极坐标中的pcolormesh

时间:2016-03-20 11:51:47

标签: python matplotlib

我试图用matplotlib绘制H原子的波函数的概率密度。我设法在笛卡尔坐标系中做到这一点,但是如果我在极坐标中指定psi,以后的计算会更好。现在我试图使绘图工作,但它给出了错误的结果(图的轴线应该是笛卡尔坐标)。知道如何解决这个问题吗?

import numpy as np
from matplotlib import pyplot as plt 
import matplotlib.cm as cm 
from scipy import integrate


Z = 1
a_0 = 1
pi = np.pi


n = 300
r = np.linspace(-10, 10, n) 
theta = np.linspace(0, 2*pi, n) 
R, Theta = np.meshgrid(r, theta) 


def psi(r,theta):
    return 1/(4*sqrt(2*pi))*(Z/a_0)**(3/2) * Z*r/a_0*np.exp(-Z*r/(2*a_0))*np.cos(theta)


X1 = R*np.cos(Theta)
X2 = R*np.sin(Theta)

plt.pcolormesh(X1,X2,psi(R,Theta)**2)
plt.axis('equal')
plt.show()

输出错误:

Incorrect output

如果我用笛卡尔坐标计算它,我得到我想要的东西:

import numpy as np
from matplotlib import pyplot as plt 
import matplotlib.cm as cm 
from scipy import integrate


Z = 1
a_0 = 1
pi = np.pi


n = 300
x1 = np.linspace(-10, 10, n) 
x2 = np.linspace(-10,10, n) 
X1, X2 = np.meshgrid(x1,x2) 


def r(x,y):
    return sqrt(x**2 + y**2)

def psi(x,y):
    return 1/(4*sqrt(2*pi))*(Z/a_0)**(3/2) * Z*r(x,y)/a_0*np.exp(-Z*r(x,y)/(2*a_0))*x/r(x,y)



plt.pcolormesh(X1,X2,psi(X1,X2)**2)

plt.axis('equal')
plt.show()

输出:

correct output

1 个答案:

答案 0 :(得分:2)

极坐标中的半径是错误的,因为它是负的,这反过来又会使你在极坐标中计算psi失败。只需更改

r = np.linspace(-10, 10, n)

r = np.linspace(0, 10, n)

解决您的问题。