geoplot.
choropleth
(df, projection=None, hue=None, scheme=None, k=5, cmap='Set1', categorical=False, vmin=None, vmax=None, legend=False, legend_kwargs=None, legend_labels=None, extent=None, figsize=(8, 6), ax=None, **kwargs)¶Area aggregation plot.
Parameters: |
|
---|---|
Returns: | The plot axis |
Return type: |
|
Examples
A choropleth takes observations that have been aggregated on some meaningful polygonal level (e.g. census tract, state, country, or continent) and displays the data to the reader using color. It is a well-known plot type, and likeliest the most general-purpose and well-known of the specifically spatial plot types. It is especially powerful when combined with meaningful or actionable aggregation areas; if no such aggregations exist, or the aggregations you have access to are mostly incidental, its value is more limited.
The choropleth
requires a series of enclosed areas consisting of shapely
Polygon
or MultiPolygon
entities, and a set of data about them that you would like to express in color. A basic choropleth requires
geometry, a hue
variable, and, optionally, a projection.
import geoplot as gplt
import geoplot.crs as gcrs
gplt.choropleth(polydata, hue='latdep', projection=gcrs.PlateCarree())
Change the colormap with the cmap
parameter.
gplt.choropleth(polydata, hue='latdep', projection=gcrs.PlateCarree(), cmap='Blues')
If your variable of interest is already categorical, you can specify categorical=True
to
use the labels in your dataset directly. To add a legend, specify legend
.
gplt.choropleth(boroughs, projection=gcrs.AlbersEqualArea(), hue='BoroName',
categorical=True, legend=True)
Keyword arguments can be passed to the legend using the legend_kwargs
argument. These arguments will be
passed to the underlying matplotlib.legend.Legend
instance (ref). The loc
and bbox_to_anchor
parameters are particularly useful for positioning the legend. Other additional arguments will be passed to the
underlying matplotlib
scatter plot.
gplt.choropleth(boroughs, projection=gcrs.AlbersEqualArea(), hue='BoroName',
categorical=True, legend=True, legend_kwargs={'loc': 'upper left'})
Additional arguments not in the method signature will be passed as keyword parameters to the underlying matplotlib Polygon patches.
gplt.choropleth(boroughs, projection=gcrs.AlbersEqualArea(), hue='BoroName', categorical=True,
linewidth=0)
Choropleths default to splitting the data into five buckets with approximately equal numbers of observations in
them. Change the number of buckets by specifying k
. Or, to use a continuous colormap, specify k=None
. In
this case a colorbar legend will be used.
gplt.choropleth(polydata, hue='latdep', cmap='Blues', k=None, legend=True,
projection=gcrs.PlateCarree())
The choropleth
binning methodology is controlled using by scheme` parameter. The default is quantile
,
which bins observations into classes of different sizes but the same numbers of observations. equal_interval
will creates bins that are the same size, but potentially containing different numbers of observations.
The more complicated fisher_jenks
scheme is an intermediate between the two.
gplt.choropleth(census_tracts, hue='mock_data', projection=gcrs.AlbersEqualArea(),
legend=True, edgecolor='white', linewidth=0.5, legend_kwargs={'loc': 'upper left'},
scheme='equal_interval')