0

I'm using a default map like:

world = geopandas.read_file(gpd.datasets.get_path('naturalearth_lowres'))

I can successfully annotate labels to this map from another GeoDataFrame (called sample_gdf here) with the following for loop sample:

for idx, row in sample_gdf.iterrows():
     plt.annotate(text=row['country_name'], # e.g. this column contains the names of each countries
                  xy=(row['longitude'], row['latitude']), # e.g. these columns are showing the coordinates of middle points of each countries
                  horizontalalignment='center')

This is how it looks like with epsg=4326 Problems are starting when I want to change the projection of the map. Default CRS for the variable 'world' above is epsg: 4326. As soon as I change the projection like this:

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world = world.to_crs(epsg=3035)

focusing on Europe, my labels are not appearing in the correct locations anymore. I've been looking for suggestions to solve this problem for a week, but couldn't find any solution for now. Thanks for your help. And this is how it looks like with epsg=3035 Labels are appearing in the left lower corner.

1 Answers1

0

It all boils down to what is stored in your ['longitude'], ['latitude'] columns you are using to position the annotation. You need to make sure that those are in the correct projection because coordinates need to reflect the actual units used on the plot.

After reprojection, get the fresh coordinates and use those.

sample_gdf["x"] = sample_gdf.centroid.x
sample_gdf["y"] = sample_gdf.centroid.y

sample_gdf.plot()

for idx, row in sample_gdf.iterrows():
     plt.annotate(text=row['country_name'], # e.g. this column contains the names of each countries
                  xy=(row['x'], row['y']), # e.g. these columns are showing the coordinates of middle points of each countries
                  horizontalalignment='center')
martinfleis
  • 7,124
  • 2
  • 22
  • 30
  • 1
    I solved the problem. When I created my `sample_gdf`, I set `crs=3050`, because I wanted this projection. But now I realised when creating a GeoDataFrame (`sample_gdf`), one have to set crs to the default according to coordinate values. Then change the crs by `sample_gdf.to_crs(epsg=3050, inplace=True)` and it works properly. – azotamiota Aug 11 '21 at 18:20