Search code examples
geopandasannotategeoplot

Annotate a geoplot when using a projection


I got a dataframe with the following columns Name (string), size (num), latitude (num), longitude (num), geometry (shapely.geometry.point.Point).

When i'm plotting my points on a map and are trying to annotate each point the annotation is not shown at all. My guess is that this is due to the projection im using.

Here are the lines of codes im running:

import geopandas as gpd
import geoplot as gplt

proj = gplt.crs.AlbersEqualArea()
fig, ax = plt.subplots(figsize=(10, 10), subplot_kw={'projection': proj})

gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.longitude, df.latitude))
gplt.pointplot(gdf, hue='size', s=15, ax=ax, cmap=palette, legend=True, zorder=10)
for idx, row in gdf.iterrows():
    plt.annotate(s=row['Name'], xy=[row['latitude'],row['longitude']])
plt.show()

Solution

  • You need coordinate transformation in

    plt.annotate(s=row['Name'], xy=[row['latitude'],row['longitude']])
    

    The transformation should be

    xtran = gplt.crs.ccrs.AlbersEqualArea()
    

    Replace that line with

    x, y = xtran.transform_point(row['longitude'], row['latitude'], ccrs.PlateCarree())
    plt.annotate( s=row['Name'], xy=[x, y] )