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)¶A simple aggregation plot based on geometry.
Parameters: |
|
---|---|
Returns: | The axis object with the plot on it. |
Return type: | AxesSubplot or GeoAxesSubplot instance |
Examples
The choropleth is a standard-bearer of the field. To make one yourself, you will need a series of enclosed areas,
consisting of shapely
Polygon
or MultiPolygon
entities, and a series 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.
gplt.choropleth(boroughs, projection=gcrs.AlbersEqualArea(), hue='BoroName', categorical=True)
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, often
necessary to properly position the legend, will be passed to the underlying matplotlib Legend instance.
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
.
gplt.choropleth(census_tracts, hue='mock_data', projection=gcrs.AlbersEqualArea(),
legend=True, edgecolor='white', linewidth=0.5, legend_kwargs={'loc': 'upper left'},
k=2)
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())
legend_labels
controls the legend labels.
gplt.choropleth(census_tracts, hue='mock_data', projection=gcrs.AlbersEqualArea(),
edgecolor='white', linewidth=0.5,
legend=True, legend_kwargs={'loc': 'upper left'},
legend_labels=['Very Low', 'Low', 'Medium', 'High', 'Very High'])
Alternatively, change the scheme used to generate the buckets with the scheme
parameter. equal_interval
,
for example, will generate buckets of equal data distribution size instead.
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')