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)¶This plot scales the size of polygonal inputs based on the value of a particular data parameter.
Parameters: |
|
---|---|
Returns: | The axis object with the plot on it. |
Return type: | GeoAxesSubplot instance |
Examples
A cartogram is a plot type which ingests a series of enclosed shapes (shapely
Polygon
or MultiPolygon
entities, in the geoplot
example) and spits out a view of these shapes in which area is distorted according
to the size of some parameter of interest. These are two types of cartograms, contiguous and non-contiguous
ones; only the former is implemented in geoplot
at the moment.
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, often
necessary to properly position the legend, will be passed to the underlying `matplotlib Legend instance
http://matplotlib.org/api/legend_api.html#matplotlib.legend.Legend`_.
gplt.cartogram(boroughs, scale='Population Density', projection=gcrs.AlbersEqualArea(),
trace=False, legend=True, legend_kwargs={'loc': 'upper left'})
Specify an alternative legend display using the legend_values
and legend_labels
parameters.
gplt.cartogram(boroughs, scale='Population Density', projection=gcrs.AlbersEqualArea(), legend=True,
legend_values=[2.32779655e-07, 6.39683197e-07, 1.01364661e-06, 1.17380941e-06, 2.33642596e-06][::-1],
legend_labels=['Manhattan', 'Brooklyn', 'Queens', 'The Bronx', 'Staten Island'],
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'})
By default, the polygons will be scaled according to the data such that the minimum value is scaled by a factor of
0.2 while the largest value is left unchanged. Adjust this 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):
def scalar(val):
return 0.5
return scalar
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
. Although
it’s possible for hue
and scale
to refer to different aspects of the data, it’s strongly recommended
to use the same data column for both. For more information on hue
-related arguments, refer to e.g. the
pointplot
documentation.
gplt.cartogram(boroughs, scale='Population Density', projection=gcrs.AlbersEqualArea(),
hue='Population Density', k=None, cmap='Blues')