Home > Software design >  Cartopy: How to add a marker on a NearsidePerspective
Cartopy: How to add a marker on a NearsidePerspective

Time:01-08

I'm trying to add a marker at the center of a map, just a small red dot. Here is the code:

import matplotlib.pyplot as plt

import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt
import cartopy.feature as cfeature


data = {
    'lat': 11.9320027,
    'lng': -86.055024,
    'time': '2022-01-01T00:33:32.401Z',
    'place': 'Catarina, Nicaragua',
}

fig = plt.figure(figsize=(5, 5), frameon=False)

projection = ccrs.NearsidePerspective(
    central_longitude=data['lng'],
    central_latitude=data['lat'],
    satellite_height=500000,
)
ax = plt.subplot(1, 1, 1, projection=projection)

ax.add_feature(cfeature.BORDERS, linewidth=0.5)
ax.add_feature(cfeature.RIVERS, linewidth=0.3)
ax.add_image(cimgt.Stamen('watercolor'), 6)

# ax.plot([point['lng']], [point['lat']], marker='.', color='red', markersize=5, alpha=0.7, transform=ccrs.Geodetic())

plt.show()

With the marker code commented out (like it currently is in the code) the image looks like this:

enter image description here

but if you uncomment the marker line - the result is not what I'd expect it to be:

enter image description here

Any idea how to fix this issue?

CodePudding user response:

UPD:

as per @RutgerKassies - this is the way to go:

ax.plot(data['lng'], data['lat'], marker='.', color='red', markersize=5, alpha=0.7, transform=ccrs.Geodetic())
ax.set_global()

Looks like plotting the red dot was messing the limits. This is the working code for plotting the red dot:

extent = ax.get_extent()
ax.plot(data['lng'], data['lat'], marker='.', color='red', markersize=5, alpha=0.7, transform=ccrs.Geodetic())
ax.set_xlim(extent[:2])
ax.set_ylim(extent[2:])
  • Related