[英]Python - Rotation of a meshgrid
I would like to work in polar coordinates with a meshgrid. 我想使用网状网格在极坐标中工作。 First I have to determine theta angle with an origin situated in the middle of the meshgrid, it s not a problem, I have splitted the meshgrid in 4 parts in order to determine the angle as you can see below. 首先,我必须确定θ角与原点位于网格网格的中间,这不是问题,我将网格划分为四个部分,以便确定角度,如下所示。
And now I would like to apply a rotation of the landmark of an angle which be chosen , so I try to change the "landmark" with "XAprim" and "YAprim" but working with the 4 parts is not easy..I think it s not the easiest way... 现在我想应用一个选定角度的地标旋转,因此我尝试用“ XAprim”和“ YAprim”更改“地标”,但是使用这四个部分并不容易..我认为这不是最简单的方法...
import numpy as np
import matplotlib.pyplot as plt
Lx=1345.
Ly=1428.
x0 = Lx/2.
y0 = Ly/2.
YA, XA = np.mgrid[0:Ly, 0:Lx]
Theta1 = np.arctan((YA-y0)/(XA-x0))
Theta2 = np.pi/2*np.ones((YAp.shape[0], YA.shape[1]))
Theta3 = 3*np.pi/2*np.ones((YA.shape[0], YA.shape[1]))
mask = np.fromfunction(lambda i, j: (i >= y0) * (j > (x0)), (XA.shape[0], XA.shape[1]), dtype=int)
test = np.invert(mask)
V1_test1 = np.ma.array(-Theta1, mask=test)
mask2 = np.fromfunction(lambda i, j: (i >= y0) * (j < (x0)), (XA.shape[0], XA.shape[1]), dtype=int)
test2 = np.invert(mask2)
V1_test2 = np.ma.array(-Theta1 - np.pi, mask=test2) #Entaille
mask3 = np.fromfunction(lambda i, j: (i < y0) * (j > (x0)), (XA.shape[0], XA.shape[1]), dtype=int)
test3 = np.invert(mask3)
V1_test3 = np.ma.array(-Theta1, mask=test3)
mask4 = np.fromfunction(lambda i, j: (i < y0) * (j <(x0)), (XA.shape[0], XA.shape[1]), dtype=int)
test4 = np.invert(mask4)
V1_test4 = np.ma.array((-Theta1 + np.pi), mask=test4) #Entaille
mask5 = np.fromfunction(lambda i, j: (i > y0) * (j==x0), (XA.shape[0], XA.shape[1]), dtype=int)
test5 = np.invert(mask5)
Theta2 = np.pi/2*np.ones((YA.shape[0], YA.shape[1]))
V1_test5 = np.ma.array(Theta2, mask=test5)
mask6 = np.fromfunction(lambda i, j: (i < y0) * (j==x0), (XA.shape[0], XA.shape[1]), dtype=int)
test6 = np.invert(mask6)
Theta3 = -np.pi/2*np.ones((YA.shape[0], YA.shape[1]))
V1_test6 = np.ma.array(Theta3, mask=test6)
a = np.ma.filled(V1_test1, 0)
b = np.ma.filled(V1_test2, 0)
c = np.ma.filled(V1_test3, 0)
d = np.ma.filled(V1_test4, 0)
e = np.ma.filled(V1_test5, 0)
f = np.ma.filled(V1_test6, 0)
theta = (a + b + c + d + e + f)
plt.imshow(theta,aspect='auto',cmap=plt.cm.hot)
plt.colorbar()
plt.show()
So I got this meshgrid which is expected : 所以我得到了这个网状网格,这是预期的:
And now, I would like to apply a rotation since the origin of the landmark As example with a 23 degrees angle, I calculate the new coordinates to do the change, but doing the same as over it because of the 4 parts of the angle...So I would like to know if there are not a more efficient way to deal this problem? 现在,由于地标的原点,我想进行旋转,例如,以23度角为例,我计算新坐标以进行更改,但是由于该角度有4个部分,因此进行了更改。 ..所以我想知道是否没有更有效的方法来解决此问题?
ang_rot = 23*np.pi/180.
XAprim = XA*np.cos(ang_rot)+YA*np.sin(ang_rot)
YAprim = -XA*np.sin(ang_rot)+YA*np.cos(ang_rot)
Theta1 = np.arctan((YAprim-y0)/(XAprim-x0))
plt.imshow(Theta1,aspect='auto',cmap=plt.cm.hot)
plt.colorbar()
plt.show()
So the solution is : 所以解决方案是:
import numpy as np
import matplotlib.pyplot as plt
Lx=1345.
Ly=1428.
x0 = Lx/2.
y0 = Ly/2.
YA, XA = np.mgrid[0:Ly, 0:Lx]
XA = XA - x0
YA = YA - y0
ang_rot = 23*np.pi/180.
XAprim = XA*np.cos(ang_rot) - YA*np.sin(ang_rot)
YAprim = XA*np.sin(ang_rot) + YA*np.cos(ang_rot)
Theta1 = np.arctan2((YAprim),(XAprim))*-180./np.pi
plt.imshow(Theta1,aspect='auto',cmap=plt.cm.hot)
plt.colorbar()
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.