Rotating an image on PYGMT

Hi everyone. I need to insert an image to the map but I need to rotate the image. The position is like a rotated rectangular. Do you know how can I do it on PYGMT?

Thanks

Hello @mtabbakhha,

you can inset figures in PyGMT using the pygmt.Figure.image method. To rotate the image you can play with the perspective parameter. For details please have a look at the documentation pygmt.Figure.image — PyGMT.

Below you find a slightly modified PyGMT gallery example:

# source: https://www.pygmt.org/latest/gallery/images/image.html#sphx-glr-gallery-images-image-py
# last access: 2022/11/09

import os

import pygmt

fig = pygmt.Figure()
fig.basemap(region=[0, 2, 0, 2], projection="X10c", frame=True)

# place and center the GMT logo from the GMT website to the position 1/1
# on a basemap, scaled up to be 3 cm wide and draw a rectangular border
# around the image
fig.image(
    imagefile="https://www.generic-mapping-tools.org/_static/gmt-logo.png",
    position="g1/1+w3c+jCM",
    box=True,
    # select perspective
    # set the azimuth and elevation angle of the viewpoint
    # Default is [180, 90]
    perspective=[90, 90],  # <<< can be used to rotate the image
)

# clean up the downloaded image in the current directory
os.remove("gmt-logo.png")

fig.show()

output figure with rotation:

1 Like

thanks so much. I have a rectangular image. I want to place the four corner of the rectangle to the four corner of gps location. Is there any way to do that?

You calculate the angle, aka the azimuth, between the « north of the plot » and the « north of your gps data » and you apply that to the perspective parameter exactly like @yvonnefroehlich explained above.