我旋转了一个矩形,并且能够在我的图形中正确绘制它。 然而,当我访问旋转矩形的坐标时,它们似乎与原始矩形相同。
如何访问更新后的坐标?
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())
最佳答案
这是因为矩形的边界框没有改变。像这里一样使用变换意味着矩形在屏幕上绘制时会发生变换。矩形本身根本不会改变。
为了获得转换矩形的坐标,您需要对坐标应用转换,如Get coordinates of the rotated patch (rectangle) matplotlib 所示。
coords = rect1.get_bbox().get_points()
print(t.transform(coords))
关于python - 更新矩形补丁的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50308610/