I'm trying to plot a set of points on a basemap using contextily in python. I have the following points object generated by shapely.geometry, long first and then lat.
0 POINT (-122.44040676 37.7452892821)
1 POINT (-122.432337016 37.7814273807)
2 POINT (-122.409302275 37.7923551768)
3 POINT (-122.446989285 37.7963790178)
4 POINT (-122.412922159 37.8076612624)
I then converted it into a geopandas data and used to following to plot it.
import contextily as ctx
ax = geo_df.plot(figsize=(10,10), alpha = 0.5)
ctx.add_basemap(ax, zoom = 10, source=ctx.providers.Stamen.TonerLite)
ax.set_axis_off()
I set the crs to geo_df = geo_df.to_crs(epsg=3857)
. The issue is that I can see the points but the basemap is not showing correctly. It seems like it is just showing them in the middle of the ocean. I suspect the basemap has a different crs or there might be an issue in my crs. Does anyone have an ideas on this? This is a full image with more points.
Assuming your geoDataFrame is created with a valid crs
, when you add_basemap
the crs
can be specified with option crs=gdf.crs.to_string()
to reproject the map images properly. Here is the relevant line of code that needs to change.
ctx.add_basemap(ax, crs=geo_df.crs.to_string(), zoom=10, source=ctx.providers.Stamen.TonerLite)