获取旋转补丁(矩形)matplotlib 的坐标
我想实现以下目标:
- 1) 获取旋转面片的坐标
- 2)获取补丁的所有点(此处为矩形)
- ** 我的印象是旋转的矩形在面之间没有 90 度.那只是可视化吗?
下面的我的代码段.但是,旋转的面片的坐标与原始面的坐标相同.如何实现1)和2)?
My snippet below. The coordinates of the rotated patch are the same as of the original though .. How to achieve 1) and 2) ?
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)
angle = np.deg2rad(45)
r1 = patches.Rectangle((8,4), 5,3, fill=False, color="red", alpha=0.50)
r2 = patches.Rectangle((8,4), 5,3, fill=False, color="blue", alpha=0.50)
trafo = mpl.transforms.Affine2D().rotate_around(8,4,angle) + ax.transData
r2.set_transform(trafo)
ax.add_patch(r1)
ax.add_patch(r2)
plt.xlim(0,15)
plt.ylim(0,15)
plt.grid(False)
plt.show()
print(r1.get_bbox())
print(r1.get_xy())
print(r2.get_bbox()) # why are they the same as for r1?
print(r2.get_xy())
#print(r1.get_all_points()) # how to achieve it?
矩形的坐标
Rectangle
是通过左下角(x,y)以及宽度和高度的坐标对定义的.要获取其角的坐标,您可以
Rectangle's coordinates
A Rectangle
is defined through a coordinate pair of the lower left corner (x,y), and a width and height. To get the coordinates of its corners, you may
-
从角落,宽度和高度计算它们
calculate them from the corner, width and height,
r1 = patches.Rectangle((8,4), 5,3)
ax.add_patch(r1)
coords = np.array([r1.get_xy(), [r1.get_x()+r1.get_width(), r1.get_y()],
[r1.get_x()+r1.get_width(), r1.get_y()+r1.get_height()],
[r1.get_x(), r1.get_y()+r1.get_height()]])
print(coords)
从转换后的路径中获取它们,
get them from the transformed path,
r1 = patches.Rectangle((8,4), 5,3)
ax.add_patch(r1)
coords = r1.get_patch_transform().transform(r1.get_path().vertices[:-1])
print(coords)
在两种情况下,打印结果均为
In both cases the printed result will be
[[ 8. 4.]
[ 13. 4.]
[ 13. 7.]
[ 8. 7.]]
您还可以从矩形的边界框获得左下角和右上角的两个点(由于一个框本身是矩形),
You may also get the two points of the lower left and upper right corner from the rectangle's bounding box (due to a box being a Rectangle itself),
r1 = patches.Rectangle((8,4), 5,3)
ax.add_patch(r1)
coords = r1.get_bbox().get_points()
print(coords)
with将导致
[[ 8. 4.]
[ 13. 7.]]
变换后的矩形的坐标.
现在,如果要变换矩形,则上述方法需要考虑到变换,以提供变换后的矩形的正确坐标.
Transformed rectangle's coordinates.
Now if you transform the rectangle, the above methods need to take the transform into account to provide the correct coordinates of the transformed rectangle.
-
转换手动获取的坐标,
transform the manually obtained coordinates,
r2 = patches.Rectangle((8,4), 5,3)
trafo = mpl.transforms.Affine2D().rotate_around(8,4,angle)
r2.set_transform(trafo + ax.transData)
ax.add_patch(r2)
coords = np.array([r2.get_xy(), [r2.get_x()+r2.get_width(), r2.get_y()],
[r2.get_x()+r2.get_width(), r2.get_y()+r2.get_height()],
[r2.get_x(), r2.get_y()+r2.get_height()]])
print(trafo.transform(coords))
变换从路径获得的坐标
transform the coordinates obtained from the path
r2 = patches.Rectangle((8,4), 5,3)
trafo = mpl.transforms.Affine2D().rotate_around(8,4,angle)
r2.set_transform(trafo + ax.transData)
ax.add_patch(r2)
coords = r2.get_patch_transform().transform(r2.get_path().vertices[:-1])
print(trafo.transform(coords))
在这些情况下,打印的坐标将是
In those cases, the printed coordinates will be
[[ 8. 4. ]
[ 11.53553391 7.53553391]
[ 9.41421356 9.65685425]
[ 5.87867966 6.12132034]]
或者,如果要从边界框中获取余项
Or, in case of getting the coodinates from the bounding box
coords = r2.get_bbox().get_points()
print(trafo.transform(coords))
印刷品
[[ 8. 4. ]
[ 9.41421356 9.65685425]]