2017-07-30 155 views
0

我正在繪製一個2d線(x1,y1) - >(x2,y2)並使用Affine2D.rotate_deg_around在matplotlib中旋轉角度theta。matplotlib旋轉後的新線座標

start = (120, 0) 
ht = 100 
coords = currentAxis.transData.transform([start[0],start[1]]) 
trans1 = mpl.transforms.Affine2D().rotate_deg_around(coords[0],coords[1], 45) 
line1 = lines.Line2D([start[0], start[0]], [start[1], ht+start[1]], color='r', linewidth=2) 
line1.set_transform(currentAxis.transData + trans1) 
currentAxis.add_line(line1) 

現在(x2,y2)在旋轉之後不會是(120,100)。我需要在旋轉後找到新的(x2,y2)。

回答

1

matplotlib轉換函數可能不是這裏最舒適的解決方案。

由於您正在旋轉並翻譯原始數據點,因此使用「公共」3 x 3旋轉矩陣和單獨的平移可能會更好。或者是一個4 x 4矩陣,同時擁有旋轉和平移。

檢查從 http://www.lfd.uci.edu/~gohlke/code/transformations.py.html

這一個返回旋轉的4×4矩陣,但你可以與翻譯右列設置上三個組成部分的功能rotation_matrix(angle, direction, point=None)

它可能看起來很可怕首先:) 但是,一旦你習慣了它是一種非常方便的工具。

多一點信息

http://www.dirsig.org/docs/new/affine.html

http://www.euclideanspace.com/maths/geometry/affine/matrix4x4/

https://en.wikipedia.org/wiki/Transformation_matrix

+0

我已經嘗試過了。它給我創造一條線時通過的值,即[120,120]和[0,100]。我需要轉化點。 –

0

改造後我找不到新的共同座標。以下是。

  1. 移位軸和圍繞旋轉點旋轉。點積的
  2. 結果

  3. 點積(上述操作,點,這需要轉換(P)的基體)旋轉後給出P的新座標

    trans1 = mpl.transforms.Affine2D().rotate_deg_around(120, 100, 45) 
    txn = np.dot(trans1.get_matrix(), [120, 200, 1]) 
    line1 = lines.Line2D([120, txn[0]], [100, txn[1]], color='r', linewidth=line_width) 
    currentAxis.add_line(line1) 
    
0

您是首先轉換爲顯示座標,然後圍繞顯示座標中的點進行旋轉。但是,我想你想要做的是在數據座標中執行旋轉,然後轉換爲顯示座標。

import matplotlib.pyplot as plt 
import matplotlib as mpl 
import matplotlib.lines as lines 
start = (120, 0) 
ht = 100 

fig, ax = plt.subplots() 

trans1 = mpl.transforms.Affine2D().rotate_deg_around(start[0],start[1], 45) 
line1 = lines.Line2D([start[0], start[0]], [start[1], ht+start[1]], color='r', linewidth=2) 
line1.set_transform(trans1 + ax.transData) 
ax.add_line(line1) 

ax.relim() 
ax.autoscale_view() 
plt.show() 

enter image description here

變換然後還可以用來獲取旋轉座標

newpoint = trans1.transform([start[0], ht+start[1]]) 
# in this case newpoint would be [ 49.28932188 70.71067812] 
+0

好的。但是我怎樣才能得到沒有變換的新座標(x2,y2)是(120,100)。改造後需要這些新點 –

+0

你是什麼意思「我需要新的點」?該線確實具有新的座標,不是嗎? – ImportanceOfBeingErnest

+0

是的,它有新的視覺效果。但我需要新的轉化點。我需要它來畫另一條線。 –