Search code examples
pythonmatplotlibcartopy

How do I get the transformed data of a cartopy geodetic plot?


how do I get all the data of the transformed line of the "handle" - Line2D object in the following code:

import cartopy.crs as ccrs
import matplotlib.pyplot as plt

ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()

ny_lon, ny_lat = -75, 43
delhi_lon, delhi_lat = 77.23, 28.61

handle = plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
         color='blue', linewidth=2, marker='o',
         transform=ccrs.Geodetic(),
         )
plt.show()

To be more clear: I'm not looking for the output of "handle[0].get_data()", since this just prints my original longitude and latitude, but im looking for the the data of the geodetic line drawn on the map.


Solution

  • I found the answer! According to this question, you can access the data of the transformation via the following code snippet:

    [handle] = plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat], color='blue', linewidth=2, marker='o', transform=ccrs.Geodetic())
    
    t_path = handle._get_transformed_path()
    
    path_in_data_coords, _ = t_path.get_transformed_path_and_affine()
    print(path_in_data_coords.vertices)
    

    In the answer to this question there is also a second approach.