利用勒让德多项式在极坐标系下建立和绘制Matplotlib二维直方图

2024-06-26 14:01:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图绘制一个分布图:

distribution of interest

这是半径为(a)的球体内部的温度分布,其上半球保持在T=1,下半球保持在T=0(忽略两个半球之间边界处的不连续性),pUl是第一类勒让德多项式。在

import pylab as pl
from scipy.special import eval_legendre as Leg
import math,sys

def sumTerm(a,r,theta,l):
    """ 
    Compute term of sum given radius of sphere (a),
    y and z coordinates, and the current index of the 
    Legendre polynomials (l) over the entire range
    where these polynomials are orthogonal [-1,1].
    """
    xRange = pl.arange(-0.99,1.0,0.01)
    x = pl.cos(theta)
    # correct for scipy handling negative indices incorrectly
    lLow = l-1
    lHigh = l+1
    if lLow < 0:
        lLow = -lLow-1
    return 0.5*((r/a)**l)*Leg(l,x)*(Leg(lLow,0)-Leg(lHigh,0))

def main():

    n = 10      # number of l terms to expand to
    a = 1.0     # radius of sphere

    # generate r, theta values
    aBins = pl.linspace(0, 2*pl.pi, 360)      # 0 to 360 in steps of 360/N.
    rBins = pl.linspace(0, 1, 50)
    theta,r = pl.meshgrid(aBins, rBins)

    tempProfile = pl.zeros([50,360])
    for nr,ri in enumerate(rBins):
        for nt,ti in enumerate(aBins):
            temp = 0.0
            for l in range(n):
                temp += sumTerm(a, ri, ti, l)
            tempProfile[nr,nt] = temp

    # plot the Temperature profile
    pl.imshow(tempProfile)
    pl.colorbar()
    pl.axes().set_aspect('equal')
    pl.show()

if __name__=='__main__':
    main()

由此得出以下曲线图:

enter image description here

在这个坐标里,我怎么能显示呢?在


Tags: ofthetoinimportformainpl