matplotlib新行协调旋转后的点

时间:2017-07-30 06:01:46

标签: python matplotlib

我正在画一条2d线(x1,y1) - > (x2,y2)并使用Affine2D.rotate_deg_around在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)。

3 个答案:

答案 0 :(得分:1)

matplotlib转换功能可能不是最舒适的解决方案。

由于您正在旋转和翻译原始数据点,因此最好使用" common" 3 x 3旋转矩阵和单独的平移。或4 x 4矩阵,包含旋转和平移。

检查功能rotation_matrix(angle, direction, point=None) http://www.lfd.uci.edu/~gohlke/code/transformations.py.html

这个矩阵返回一个4 x 4矩阵进行旋转,但你可以在右栏中设置上面三个组件并进行平移。

起初可能看起来很可怕:) 但是一旦你习惯了,它就是一种非常方便的工具。

更多信息

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

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

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

答案 1 :(得分:0)

转换后我无法得到新的坐标。以下是如此。

  1. 移动轴并围绕旋转点旋转。
  2. 点积(上述操作的矩阵,需要转换的点(P))
  3. 点积的结果给出了旋转后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)
    

答案 2 :(得分: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]