geoplot.
cartogram
(df, projection=None, scale=None, limits=(0.2, 1), scale_func=None, trace=True, trace_kwargs=None, hue=None, categorical=False, scheme=None, k=5, cmap='viridis', vmin=None, vmax=None, legend=False, legend_values=None, legend_labels=None, legend_kwargs=None, legend_var='scale', extent=None, figsize=(8, 6), ax=None, **kwargs)¶Self-scaling area plot.
Parameters: |
|
---|---|
Returns: | The plot axis |
Return type: |
|
Examples
A cartogram is a plot type which ingests a series of enclosed Polygon
or MultiPolygon
entities and spits
out a view of these shapes in which area is distorted according to the size of some parameter of interest.
A basic cartogram specifies data, a projection, and a scale
parameter.
import geoplot as gplt
import geoplot.crs as gcrs
gplt.cartogram(boroughs, scale='Population Density', projection=gcrs.AlbersEqualArea())
The gray outline can be turned off by specifying trace
, and a legend can be added by specifying legend
.
gplt.cartogram(boroughs, scale='Population Density', projection=gcrs.AlbersEqualArea(),
trace=False, 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.cartogram(boroughs, scale='Population Density', projection=gcrs.AlbersEqualArea(),
trace=False, legend=True, legend_kwargs={'loc': 'upper left'})
Additional arguments to cartogram
will be interpreted as keyword arguments for the scaled polygons,
using matplotlib Polygon patch rules.
gplt.cartogram(boroughs, scale='Population Density', projection=gcrs.AlbersEqualArea(),
edgecolor='darkgreen')
Manipulate the outlines use the trace_kwargs
argument, which accepts the same matplotlib Polygon patch parameters.
gplt.cartogram(boroughs, scale='Population Density', projection=gcrs.AlbersEqualArea(),
trace_kwargs={'edgecolor': 'lightgreen'})
Adjust the level of scaling to apply using the limits
parameter.
gplt.cartogram(boroughs, scale='Population Density', projection=gcrs.AlbersEqualArea(),
limits=(0.5, 1))
The default scaling function is linear: an observations at the midpoint of two others will be exactly midway
between them in size. To specify an alternative scaling function, use the scale_func
parameter. This should
be a factory function of two variables which, when given the maximum and minimum of the dataset,
returns a scaling function which will be applied to the rest of the data. A demo is available in
the example gallery.
def trivial_scale(minval, maxval): return lambda v: 2
gplt.cartogram(boroughs, scale='Population Density', projection=gcrs.AlbersEqualArea(),
limits=(0.5, 1), scale_func=trivial_scale)
cartogram
also provides the same hue
visual variable parameters provided by e.g. pointplot
. For more
information on hue
-related arguments, see the related sections in the pointplot
documentation.
gplt.cartogram(boroughs, scale='Population Density', projection=gcrs.AlbersEqualArea(),
hue='Population Density', k=None, cmap='Blues')