pythonseaborngeopandaskernel-densitygeoplot

Geoplot not displaying x,y axis python


I'm trying to use geoplot to draw a heatmap of crime distribution in San Francisco county. The problem is when I used the dataset from geoplot to draw the map of SF, the x,y axes do not appear. I'm using this map for my final presentation, so I figured it would be more readable for my audiences with axes displayed for them(longitude&latitude as axes). I've searched as many documents as I could, and I tried to use ax.axis('on') but didn't work. I would be much appreciate for any advice given. Thanks (BTW I'm doing this on jupyter) df is the data frame I used which contained 500k crime records with locations containing longitude & latitude.

gdf1 = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.Longitude, df.Latitude))
sf = gpd.read_file(gplt.datasets.get_path("san_francisco"))
count = gdf1
ax = gplt.polyplot(sf, projection=gcrs.AlbersEqualArea(),figsize=(10,10))
gplt.kdeplot(count, cmap="Reds", shade=True, clip=sf, ax=ax, cbar=True)

enter image description here


Solution

  • Geoplot is built on top of cartopy, and the returned axis objects do seem to be normal cartopy GeoAxis objects, so you should be able to use any figure customizations available within that library. By default cartopy plots do not include axis ticks/ticklabels because the library is frequently used to plot in a projection, where the x and y values do not correspond to lat/lon.

    To add gridlines in cartopy, use ax.gridlines. These will correctly handle your projection and draw lat/lon lines as contours on your map.

    From the docs on adding map gridlines and tick labels:

    ax.gridlines(draw_labels=True, dms=True, x_inline=False, y_inline=False)
    

    There are a ton of arguments to gridlines and ways to customize them but hopefully that gets you started.