Skip to content Skip to sidebar Skip to footer

Get Coordinates Of The Rotated Patch (rectangle) Matplotlib

I want to achieve the following: 1) get coordinates of the rotated patch 2) get all points of the patch (here: rectangle) ** I have an impression that the rotated rectangle does n

Solution 1:

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 will result in

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

prints

[[ 8.          4.        ]
 [ 9.41421356  9.65685425]]

Post a Comment for "Get Coordinates Of The Rotated Patch (rectangle) Matplotlib"