I have a plot that uses a Cartopy map for its coordinate system. I want to click anywhere on the plot, mark it, and return the location of the clicked point in terms of Latitude and Longitude. I am just not sure how to transform the event data back to map coordinates.
I have read through the Matplotlib Transformations Tutorial but it is still unclear to me. The Navigation Toolbar on the plot even displays the coordinates in Lat./Lon but I'm not sure how to access that.
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
fig = plt.figure()
ax = plt.axes(projection=ccrs.Sinusoidal())
ax.set_extent([0,10,0,10], crs=ccrs.PlateCarree())
ax.gridlines(draw_labels=True,dms=True,x_inline=False,y_inline=False)
def onclick(event):
ax.scatter(event.xdata, event.ydata)
fig.canvas.draw()
# I want to print xdata and ydata in terms of Latitude and Longitude
print(event.xdata,event.ydata)
cid = fig.canvas.mpl_connect('button_press_event', onclick)

To convert the values back to latitude and longitude floats, simply use the
transform_pointmethod on thePlateCarreeprojection. Here I formatted the values using the formatters from the gridliner, but you could obviously format how you like.