如何在python中将二维正态分布转换为二维lognorm分布

时间:2018-10-25 02:56:16

标签: python matplotlib scipy

我的问题有两个部分,一个是我如何定义一个二维的lognorm函数,该函数接受x和y位置并返回lognorm输出。然后,我想获取此输出并将其用作3d表面图中的z尺寸。因此,我的目标是将二维lognorm分布绘制为3d表面图。任何帮助都非常感谢。

2 个答案:

答案 0 :(得分:0)

这是要点,我尚未测试,但逻辑并不算太复杂。它也可以向量化以加快速度,但是您必须将linspace映射到单个2D向量长数组中。

dx = xMax - xMin
dy = yMax - yMin
c= (dx+dx/2.0,dy+dy/2.0)
z=np.array(shape=(N,M))
#Lets use a cartersian input vector space instead of polar
for i in range(len(xlinspace)):
    for j in range(len(ylinspace)):
        p=(xlinspace[i],ylinspace[j])
        d= np.norm(p-c) # produce a distance form the centre for the smapled point
        t=d #I'm just renaming distance to t to make it clear, this is an extra step as far as the program is concerned.
        #Now put your lognorm logic here, i'll use f as a placeholder function
        z[i][j]=f(t)
#You now have a 2d array of z outputs

答案 1 :(得分:0)

dx = 90 - (-90)
dy = 90 - (-90)
c = [dx + dx/2.0, dy+dy/2.0]
z = np.zeros((400, 400))
x = np.linspace(-90, 90, 400)
y = x.copy()
for i in range(len(x)):
    for j in range(len(y)):
        p =[x[i], y[j]]
        d = math.sqrt((p[0]-c[0])**2 + (p[1]-c[1])**2)
        t = d

        z[i][j] = lognorm.pdf(t, 1.2)




fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.plot_surface(x,y, z, cmap = 'viridis')



plt.show()

好的,这是我编写的代码,输出肯定可以到达那里,但看起来有点奇怪。我已经为您附上了它,请查看是否可以提供任何指导。enter image description here