2017-07-30 154 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

是的,它有新的视觉效果。但我需要新的转化点。我需要它来画另一条线。 –