【问题标题】:Update the position of the rectangle patch更新矩形补丁的位置
【发布时间】:2018-05-12 16:52:21
【问题描述】:

我旋转了一个矩形,并且能够在我的图中正确地绘制它。 但是,当我访问旋转矩形的坐标时,它们似乎与原始坐标相同。

如何获取更新后的坐标?

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib as mpl

from matplotlib.transforms import Affine2D

fig = plt.figure()

ax = fig.add_subplot(111)

cx, cy = 6,7
l, w = 3,4
angle = np.deg2rad(30)

ts = ax.transData
t = mpl.transforms.Affine2D().rotate_around(cx,cy,angle)
tr =  t + ts

rect0 = patches.Rectangle((cx - l/2.0,
                        cy - w/2.0), l,w, fill=False, color="blue", alpha=0.50)
rect1 = patches.Rectangle((cx - l/2.0,
                        cy - w/2.0), l,w, color="blue",  alpha=0.50, transform=tr)


print(rect0.get_bbox())
print(rect1.get_bbox())

【问题讨论】:

    标签: python matplotlib rotation patch


    【解决方案1】:

    这是因为矩形的边界框没有改变。像您在此处使用的变换意味着在屏幕上绘制矩形时会对其进行变换。矩形本身根本不会改变。

    为了获得变换后矩形的坐标,您需要对坐标应用变换,如Get coordinates of the rotated patch (rectangle) matplotlib所示

    coords = rect1.get_bbox().get_points()
    print(t.transform(coords))
    

    【讨论】:

      猜你喜欢
      • 2021-09-28
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-07
      • 2020-04-07
      相关资源
      最近更新 更多