简体   繁体   中英

Python: heat density plot in a disk

My goal is to make a density heat map plot of sphere in 2D. The plotting code below the line works when I use rectangular domains. However, I am trying to use the code for a circular domain. The radius of sphere is 1. The code I have so far is:

from pylab import *
import numpy as np
from matplotlib.colors import LightSource
from numpy.polynomial.legendre import leggauss, legval

xi = 0.0
xf = 1.0
numx = 500

yi = 0.0
yf = 1.0
numy = 500


def f(x):
    if 0 <= x <= 1:
        return 100
    if -1 <= x <= 0:
        return 0


deg = 1000
xx, w = leggauss(deg)
L = np.polynomial.legendre.legval(xx, np.identity(deg))
integral = (L * (f(x) * w)[None,:]).sum(axis = 1)

c = (np.arange(1, 500) + 0.5) * integral[1:500]


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


theta = np.arctan2(y, x)
x, y = np.linspace(0, 1, 500000)


def T(x, y):
    return (sum(r(x, y) ** l * c[:,None] *
                np.polynomial.legendre.legval(xx, identity(deg)) for l in range(1, 500)))

T(x, y) should equal the sum of c the coefficients times the radius as a function of x and y to the l power times the legendre polynomial where the argument is of the legendre polynomial is cos(theta) .

In python: integrating a piecewise function , I learned how to use the Legendre polynomials in a summation but that method is slightly different, and for the plotting, I need a function T(x, y) .


This is the plotting code.

densityinterpolation = 'bilinear'
densitycolormap = cm.jet
densityshadedflag = False
densitybarflag = True
gridflag = True
plotfilename = 'laplacesphere.eps'

x = arange(xi, xf, (xf - xi) / (numx - 1))
y = arange(yi, yf, (yf - yi) / (numy - 1))
X, Y = meshgrid(x, y)
z = T(X, Y)


if densityshadedflag:
    ls = LightSource(azdeg = 120, altdeg = 65)
    rgb = ls.shade(z, densitycolormap)
    im = imshow(rgb, extent = [xi, xf, yi, yf], cmap = densitycolormap)
else:
    im = imshow(z, extent = [xi, xf, yi, yf], cmap = densitycolormap)
im.set_interpolation(densityinterpolation)
if densitybarflag:
    colorbar(im)


grid(gridflag)

show()

I made the plot in Mathematica for reference of what my end goal is 在此处输入图片说明

如果将磁盘域(或所需域)之外的值设置为float('nan'),则在绘制时这些点将被忽略(将其保留为白色)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM