geoplot.
kdeplot
(df, projection=None, extent=None, figsize=(8, 6), ax=None, clip=None, **kwargs)¶Spatial kernel density estimate plot.
Parameters: |
|
---|---|
Returns: | The plot axis |
Return type: |
|
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)
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)
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)
Or specify cmap
to change the colormap.
ax = gplt.kdeplot(collisions, projection=gcrs.AlbersEqualArea(),
cmap='Purples')
gplt.polyplot(boroughs, projection=gcrs.AlbersEqualArea(), ax=ax)
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)