Python matplotlib新线坐标旋转后的点

Python matplotlib新线坐标旋转后的点,python,matplotlib,Python,Matplotlib,我正在绘制一条二维线(x1,y1)->(x2,y2),并使用Affine2D在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

我正在绘制一条二维线(x1,y1)->(x2,y2),并使用Affine2D在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)在旋转后不会是(120100)。我需要在旋转后找到新的(x2,y2)。

matplotlib转换函数可能不是最合适的解决方案

由于您正在旋转和平移原始数据点,因此最好使用“通用”3 x 3旋转矩阵和单独的平移。或者一个4 x 4矩阵,同时包含旋转和平移

检查功能
旋转矩阵(角度、方向、点=无)
from

这个函数返回一个4 x 4的旋转矩阵,但您可以使用平移设置右列中的上三个分量

一开始可能看起来很吓人:) 但一旦你习惯了,它是一个非常方便的工具

更多信息


转换后,我无法获得新坐标。下面的例子也是如此

  • 移动轴并围绕旋转点旋转
  • 点积(上述运算的矩阵,需要变换的点(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)
    

  • 首先转换为显示坐标,然后围绕显示坐标中的点旋转。但是,我认为您要做的是在数据坐标中执行旋转,然后转换为显示坐标

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

    然后还可以使用变换来获得旋转坐标

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

    我已经试过了。它给出了创建一行时传递的值,即[120120]和[0100]。我需要变换点,好的,但我如何得到(x2,y2)的新坐标,没有变换的坐标是(120100)。转型后需要这些新点你说的“我需要新点”是什么意思?这条线确实有新的坐标,不是吗?是的,它有新的点,可以从视觉上看到。但我需要新的转换点。我需要它来画另一条线。你可以使用
    trans1
    来获得坐标,请参阅更新的答案。