Python错误中的流图

Python错误中的流图,python,python-3.x,numpy,matplotlib,Python,Python 3.x,Numpy,Matplotlib,我想知道如何修复以下代码中的错误 import numpy as np import matplotlib.pyplot as plt from sympy.functions.special.polynomials import assoc_legendre from scipy.misc import factorial, derivative import sympy as sym def main(): t = 36000 a=637000000 H=200

我想知道如何修复以下代码中的错误

import numpy as np
import matplotlib.pyplot as plt
from sympy.functions.special.polynomials import assoc_legendre
from scipy.misc import factorial, derivative
import sympy as sym

def main():
    t = 36000
    a=637000000
    H=200
    g=9.81
    x = sym.symbols('x')

    for l in range(1, 6):
        ω=np.sqrt(g*H*l*(l+1))/a

        for n in range(l+1):
            nθ, nφ = 128, 256
            θ, φ = np.linspace(0, np.pi, nθ), np.linspace(0, 2*np.pi, nφ)
            legfun_sym = sym.functions.special.polynomials.assoc_legendre(l, n, x)
            legfun_num = sym.lambdify(x,legfun_sym)

            X, Y = np.meshgrid(θ, φ)

            uθ = (g/(a*ω))*Der_Assoc_Legendre(legfun_num, l, n, X)*np.sin(n*Y-ω*t)
            uφ = (g/(a*ω*np.sin(X)))*Assoc_Legendre(l, n, X)*np.cos(n*Y-ω*t)
            #speed = np.sqrt(uθ**2 + uφ**2)

            fig0, ax = plt.subplots()
            strm = ax.streamplot(φ, θ, uφ, uθ, linewidth=2, cmap=plt.cm.autumn)
            fig0.colorbar(strm.lines)
            plt.show()

def Assoc_Legendre(m, n, X):
    L=[]
    for i in X:
        k=[]
        for j in i:
            k.append(assoc_legendre(m, n, np.cos(j)))
        L.append(k)
    return np.array(L)

def Der_Assoc_Legendre(legfun_num, m, n, X):
    L=[]
    for i in X:
        k=[]
        for j in i:
            k.append(derivative(legfun_num, j, dx=1e-7))
        L.append(k)
    return np.array(L)

if __name__=='__main__':
    main()
错误消息“u”和“v”必须为“栅格(x,y)”形状出现在
strm=ax.流线图(φ,θ,uφ,uθ,线宽=2,cmap=plt.cm.秋季)
线条上。我该如何解决这个问题

作为参考,我试着做一个$u{{\theta}和$u{{\phi}的流线图作为参考,我试着做一个$u{{\theta}和$u{{\theta}{{\theta}和$u{{\theta}{{\omega a}{\Omegaa}作为参考,我试着做一个$u{{\theta a}{{\omega a}{a}\omega a}\a}\a}\fra{a}\fra{{a}\fra{d{d{d}\d{d{d}{d\theta\theta}\theta}\d}\theta}\theta}\d{d}\theta}\d}\theta}{d}\d}\d}\theta}\theta}\theta}\d{d}\theta})$

编辑:

这是我的当前代码:

import numpy as np
import matplotlib.pyplot as plt
from sympy.functions.special.polynomials import assoc_legendre
from scipy.misc import factorial, derivative
import sympy as sym

def main():
    t = 36000
    a=637000000
    H=200
    g=9.81
    x = sym.symbols('x')
    X, Y = np.mgrid[0.01:np.pi-0.01:100j,0:2*np.pi:100j]

    for l in range(1, 6):
        ω=np.sqrt(g*H*l*(l+1))/a

        for n in range(l+1):
            #nθ, nφ = 128, 256
            #θ, φ = np.linspace(0.001, np.pi-0.001, nθ), np.linspace(0, 2*np.pi, nφ)
            legfun_sym = sym.functions.special.polynomials.assoc_legendre(l, n, x)
            legfun_num = sym.lambdify(x, legfun_sym)
            uθ = (g/(a*ω*np.sin(X)))*Der_Assoc_Legendre(legfun_num, l, n, X)*np.sin(n*Y-ω*t)
            uφ = (g/(a*ω))*Assoc_Legendre(l, n, X)*np.cos(n*Y-ω*t)
            #speed = np.sqrt(uθ**2 + uφ**2)
            fig0, ax = plt.subplots()
            strm = ax.streamplot(Y, X, uθ,uφ, linewidth=0.5, cmap=plt.cm.autumn)
            #fig0.colorbar(strm.lines)
            plt.show()
            print("next")

def Assoc_Legendre(m, n, X):
    L=[]
    for i in X:
        k=[]
        for j in i:
            k.append(assoc_legendre(m, n, np.cos(j)))
        L.append(k)
    return np.float64(np.array(L))

def Der_Assoc_Legendre(legfun_num, m, n, X):
    L=[]
    for i in X:
        k=[]
        for j in i:
            k.append(derivative(legfun_num, j, dx=0.001))
        L.append(k)
    return np.float64(np.array(L))

if __name__=='__main__':
    main()

当前的问题似乎与
Der_Assoc_Legendre
中的导数函数有关,该函数在绘制第一个绘图和第二个绘图后会出现错误
ValueError:math domain error

而python 3允许您在变量名中使用希腊字符,我可以向您保证,大多数程序员都会发现您的代码不可读,而其他人维护/开发充斥着
φ
θ
的代码将是一场噩梦

其次,您的代码会快速抛出一个与除法为零有关的
运行时警告,您应该非常安全地跟踪并修复它

至于你的问题,问题是双重的。第一个问题是,在调用
streamline
时,输入的维度不匹配:

>>> print(φ.shape, θ.shape, uφ.shape, uθ.shape)
(256,) (128,) (256, 128) (256, 128)
诀窍在于,许多
matplotlib
plot函数期望其2d数组维度发生变换,这与
numpy.meshgrid
的奇怪定义密切相关:

>>> i,j = np.meshgrid(range(3),range(4))
>>> print(i.shape)
(4, 3)
可能由于这个原因,以下情况:

Axes.streamplot(ax、*args、**kwargs)
绘制矢量流的流线。
x、 y:1d阵列
均匀分布的网格。
u、 v:2d阵列
x和y速度。行数应匹配y的长度,列数应匹配x。
注意关于尺寸的最后一点。所有你需要做的是交换x/y,或转置角度;您需要检查其中哪一个将在应用程序中生成更有意义的绘图

现在,如果修复此问题,将发生以下情况:

TypeError:输入类型不支持ufunc“isfinite”,并且无法根据强制转换规则“safe”将输入安全强制为任何受支持的类型

现在这是可疑的。所有输入类型都应该是数字…对吗?嗯,是的,但他们不是:

>>> print(φ.dtype, θ.dtype, uφ.dtype, uθ.dtype)
float64 float64 object float64
第三个
对象
类型的数组是关于什么的

>>> print(uφ[0,0],uθ[0,0])
+inf -0.00055441014491
>>> print(type(uφ[0,0]),type(uθ[0,0]))
<class 'sympy.core.numbers.Float'> <class 'numpy.float64'>
打印(uφ[0,0],uθ[0,0]) +inf-0.00055441014491 >>>打印(类型(uφ[0,0]),类型(uθ[0,0]))

作为。上述类型的
是使用sympy构造的直接结果。如果在构建结果数组之前将这些sympy浮点转换为python或numpy浮点,那么
dtype
问题应该消失。剩余的无穷大(在
1/sin(X)
中被0除的结果)是否会被
流图
优雅地处理是另一个问题。

尽管在0和$\pi$处被
np.sin(X)
除的结果不是很好,在我看来,
dtype
步骤与
Assoc\u Legendre
函数和
Der\u Assoc\u Legendre
函数的区别在于。我不太清楚如何修理this@Jelmes你是对的,我不知怎么地忽略了只有有问题的一个是用sympy计算的。因此,相反地,
append(float(assoc_legendre(…)))
,但你仍然会有无穷大(我们将看到
streamplot
如何处理这些)。如果你看到编辑过的文章,它会给出一个参考,说明我实际上正在尝试做什么。乳胶还没印出来,真烦人。可能是错误造成的?无论如何,希望你能读到:)