-1

I would like to annotate the city name Berlin at the coordinates xy=(52.52, 13.405). I've tried ax.annotate() which yields a strange map. Maybe it has to do with the CRS of the coordinates?

import geopandas as gpd
import contextily as ctx

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world = world[(world.name=="Germany")]
world = world.to_crs(epsg=3857)

ax = world.plot(figsize=(10, 10), color='none', linewidth=1, alpha=0.5)
ax.annotate("Berlin", xy=(52.52, 13.405))

ctx.add_basemap(ax, url=ctx.providers.Stamen.Watercolor, zoom=9)

enter image description here

Stücke
  • 868
  • 3
  • 14
  • 41
  • 1
    Not familiar with `geopandas` but in `matplotlib.pyplot` the syntax of `annotate` is `ax.annotate('text',(x,y))`. I guess `geopandas` is written over `matplotlib`. Try this `ax.annotate('Berlin',xy)` – Ch3steR May 25 '20 at 08:51

2 Answers2

2

According to Annotations docpage your code should look like this:

ax.annotate("Berlin", xy=(52.52, 13.405))
Aleksey
  • 775
  • 5
  • 14
1

My initial code was flawed in two ways. I wrongly used ax.annoate() as pointed out by other answers.

In addition, world was transformed to espg=3857. The coordinates of the city Berlin weren't. With transformed coordinates it works:

import geopandas as gpd
import contextily as ctx

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world = world[(world.name=="Germany")]
world = world.to_crs(epsg=3857)

ax = world.plot(figsize=(10, 10), color='none', linewidth=1, alpha=0.5)
ax.annotate("Berlin", xy=(1491636.9565986055, 6895388.533179172))

ctx.add_basemap(ax, url=ctx.providers.Stamen.Watercolor, zoom=9)

enter image description here

Stücke
  • 868
  • 3
  • 14
  • 41