geoplot.kdeplot

geoplot.kdeplot(df, projection=None, extent=None, figsize=(8, 6), ax=None, clip=None, **kwargs)

Spatial kernel density estimate plot.

Parameters:
  • df (GeoDataFrame) – The data being plotted.
  • projection (geoplot.crs object instance, optional) – A geographic projection. For more information refer to the tutorial page on projections.
  • clip (None or iterable or GeoSeries, optional) – If specified, the kdeplot output will be clipped to the boundaries of this geometry.
  • extent (None or (minx, maxx, miny, maxy), optional) – Used to control plot x-axis and y-axis limits manually.
  • figsize (tuple, optional) – An (x, y) tuple passed to matplotlib.figure which sets the size, in inches, of the resultant plot.
  • ax (AxesSubplot or GeoAxesSubplot instance, optional) – A matplotlib.axes.AxesSubplot or cartopy.mpl.geoaxes.GeoAxesSubplot instance. Defaults to a new axis.
  • kwargs (dict, optional) – Keyword arguments to be passed to the underlying seaborn kernel density estimate plot.
Returns:

The plot axis

Return type:

AxesSubplot or GeoAxesSubplot

Examples

Kernel density estimate is a flexible unsupervised machine learning technique for non-parametrically estimating the distribution underlying input data. The KDE is a great way of smoothing out random noise and estimating the true shape of point data distributed in your space, but it needs a moderately large number of observations to be reliable.

The geoplot kdeplot, actually a thin wrapper on top of the seaborn kdeplot, is an application of this visualization technique to the geospatial setting.

A basic kdeplot specifies (pointwise) data and, optionally, a projection. To make the result more interpretable, I also overlay the underlying borough geometry.

ax = gplt.kdeplot(collisions, projection=gcrs.AlbersEqualArea())
gplt.polyplot(boroughs, projection=gcrs.AlbersEqualArea(), ax=ax)
_images/kdeplot-overlay.png

Most of the rest of the parameters to kdeplot are parameters inherited from the seaborn method by the same name, on which this plot type is based. For example, specifying shade=True provides a filled KDE instead of a contour one:

ax = gplt.kdeplot(collisions, projection=gcrs.AlbersEqualArea(),
                  shade=True)
gplt.polyplot(boroughs, projection=gcrs.AlbersEqualArea(), ax=ax)
_images/kdeplot-shade.png

Use n_levels to specify the number of contour levels.

ax = gplt.kdeplot(collisions, projection=gcrs.AlbersEqualArea(),
                  n_levels=30)
gplt.polyplot(boroughs, projection=gcrs.AlbersEqualArea(), ax=ax)
_images/kdeplot-n-levels.png

Or specify cmap to change the colormap.

ax = gplt.kdeplot(collisions, projection=gcrs.AlbersEqualArea(),
     cmap='Purples')
gplt.polyplot(boroughs, projection=gcrs.AlbersEqualArea(), ax=ax)
_images/kdeplot-cmap.png

Oftentimes given the geometry of the location, a “regular” continuous KDEPlot doesn’t make sense. We can specify a clip of iterable geometries, which will be used to trim the kdeplot. Note that if you have set shade=True as a parameter you may need to additionally specify shade_lowest=False to avoid inversion at the edges of the plot.

gplt.kdeplot(collisions, projection=gcrs.AlbersEqualArea(),
             shade=True, clip=boroughs)
_images/kdeplot-clip.png