有没有办法绘制坐标非单调递增的颜色图?颜色图的映射

问题描述 投票:0回答:1

(Python)

对于一个项目,我正在研究从一个复杂坐标系到另一个复杂坐标系的映射。在初始系统(x/y 平面,z=x+iy)中,我定义了一个势。映射后,我想在新的坐标系(u/v 平面,w=u+iv)中绘制相同的势。 我使用 colormaps/contourf 来可视化电势,并且它在 x/y 平面上工作得非常好,但是,在将电势的坐标映射到正方形/单调增加的网格中之后,它就不再存在了。这会破坏颜色图。是否有一个不需要方坐标/单调递增的函数,可以插入正确的颜色?或者有人知道我如何自己编程吗?

如下图所示,颜色图的转换方式与线条的转换方式不同。这些线的映射方式与颜色图坐标相同,并且它们工作正常,所以我认为我的代码背后的想法是可靠的,这只是轮廓/颜色图函数的错误输入。

What colormap gives me. I want the colormap to follow the transformation

代码的重要部分,pot是我的潜力,g(z)是我从z到w的映射:

from matplotlib import pyplot as plt
import numpy as np

xdomain = [-np.pi,np.pi]
ydomain = [0,3]
udomain = [-4,4]
vdomain = [-4,4]
gridsize = 20

#potential function
def pot(z):
    return np.imag(z)

#conformal mapping from z to w
def g(z):
    return np.sin(z)

#x's and y's for the colormap grid
x = np.linspace(xdomain[0]/2,xdomain[1]/2,gridsize)
y = np.linspace(ydomain[0],ydomain[1],gridsize)
w = g(x+1j*y)
z = np.array([pot(i+1j*j) for j in y for i in x])
Z = z.reshape(gridsize,gridsize)

fig, ax = plt.subplots(2,2)
ax[0][0].contourf(x,y, Z)
ax[1][0].contourf(np.real(w),np.imag(w),Z)

ax[0][0].set_xlim(xdomain)
ax[0][0].set_ylim(ydomain)
ax[0][0].set_title('z-plane')

ax[1][0].set_xlim(udomain)
ax[1][0].set_ylim(vdomain)
ax[1][0].set_title('w-plane')

有时我会收到以下警告(当我尝试使用 pcolormesh 命令而不是轮廓命令时发生这种情况):

C:\###\conf_map.py:103: UserWarning: The input coordinates to pcolormesh
are interpreted as cell centers, but are not monotonically increasing 
or decreasing. This may lead to incorrectly calculated cell edges,
in which case, please supply explicit cell edges to pcolormesh.
ax[1][0].pcolormesh(np.real(w),np.imag(w),Z, shading='auto')

我尝试将轮廓函数更改为不同的颜色图函数(图像、pcolormesh 等,但似乎都有相同的问题)

python mapping colormap coordinate-transformation
1个回答
0
投票

我发现我的坐标系转换错误。现在可以了! 'w=g(x+1j*y)' 行必须替换为:

U, V = np.meshgrid(x,y)
w = g(U+1j*V)

This results in this plot

我还更改了热图类型(但它是双向的):

ax[0][0].pcolormesh(x,y, Z, shading='gouraud', cmap='RdBu')
ax[1][0].pcolormesh(np.real(w),np.imag(w),Z, shading='gouraud', cmap='RdBu')

对于那些想知道如何绘制网格的人(@rioV8),我首先制作绘制正常网格的曲线,然后对它们进行变换。其代码如下所示(下面应该可以在没有其他代码的情况下工作):

from matplotlib import pyplot as plt
import numpy as np

xdomain = [0,2]
ydomain = [0,2]
mapsteps = 20

steps = 50
axxes = [21,31]

#mapping
def g(z):
    return z**2

#axxes[0] amount of x-axes drawn with steps amount of points
xaxes = np.linspace(xdomain[0],xdomain[1],axxes[0]) #real part of input
ystep = np.linspace(ydomain[0],ydomain[1],steps) #imaginary part of input
Xax, Yst = np.meshgrid(xaxes,ystep)

#Same for y-axes
xstep = np.linspace(xdomain[0],xdomain[1],steps)
yaxes = np.linspace(ydomain[0],ydomain[1],axxes[1])
Yax, Xst = np.meshgrid(yaxes,xstep)

#transform x and y-axes. calling them u and v is quite a misnomer
#w = u + iv
u = g(Xst + 1j*Yax) 
v = g(Xax + 1j*Yst) #Plugging the real and imaginary parts into the function

fig, ax = plt.subplots(2,4)

#Draw the normal grid
[ax[0][0].plot(Xst[:,i], Yax[:,i], color='gray', linewidth=0.5)  for i in range(len(Yax[0]))]
[ax[0][0].plot(Xax[:,i], Yst[:,i], color='gray', linewidth=0.5)  for i in range(len(Xax[0]))]
ax[0][0].set_xlim(xdomain)
ax[0][0].set_ylim(ydomain)
ax[0][0].set_title('z-plane')

#draw the transformed grid
xts = ax[1][0].plot(np.real(u),np.imag(u), color='gray', linewidth=0.5)#, label=xaxes)
yts = ax[1][0].plot(np.real(v),np.imag(v), color='gray', linewidth=0.5)
ax[1][0].set_title('w-plane')
© www.soinside.com 2019 - 2024. All rights reserved.