2014-12-04 74 views
0

我想用meshgrid在極座標系下工作。 首先,我必須確定theta角度,其中位於網格中間的 ,這不是問題,我已經在 中分割了4個網格中的網格,以確定角度,如下所示。Python - 旋轉meshgrid

現在我想申請一個角度的地標的旋轉被選中,所以我嘗試用「XAprim」和「YAprim」來改變這個「里程碑」,但是使用4個部分並不容易。 。我認爲這不是最簡單的方法...

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() 

所以我得到這個meshgrid預計: enter image description here

而現在,我想申請以來具有里程碑意義的原點旋轉 作爲23度角的例子,我計算新的座標來做改變,但是做th因爲角度的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() 
+1

不能肯定我明白你的問題,但你在numpy的包看了一下'arctan2'代替arctan'的' ?當您提供x和y座標時,這會計算出正確的象限。 – Tony 2014-12-04 08:23:28

+0

我不知道這個命令!它運作良好。謝謝;) – user3601754 2014-12-04 08:29:03

+0

但我的起源被翻譯了...我一定有一個錯誤 – user3601754 2014-12-04 08:31:39

回答

1

因此,解決辦法是:

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() 

enter image description here

+0

很好解決。你可以添加最終圖片 - 感興趣的是看看它的樣子。 – Tony 2014-12-04 09:02:25

+0

它看起來非常漂亮。 :-) – Tony 2014-12-05 01:00:45

+0

是的!它用於識別描述裂縫運動學的參數! :) – user3601754 2014-12-06 08:10:53