Plotly Choropleths¶
This example reviews plotting a choropleth from a GeoDataFrame using plotly.
[1]:
import mapped
import geopandas as gpd
import plotly
mapped.__version__, gpd.__version__, plotly.__version__
[1]:
('20.4.2', '0.7.0', '4.6.0')
First load some example polygon data.
[2]:
gdf = gpd.read_file(gpd.datasets.get_path('nybb'))
gdf['Population'] = [
479_458,
2_358_582,
2_648_771,
1_664_727,
1_471_160,
]
gdf.set_index('BoroName', inplace=True)
Importing mapped
monkeypatches geopandas
to add several tools to the geopandas.GeoDataFrame
class, including plotly_choropleth
.
[3]:
gdf.plotly_choropleth('Population')
For another example, let’s plot the same election results data as found in the plotly express mapbox choropleth example. We can load this example data as a GeoDataFrame from the mapped.example_data module.
[4]:
from mapped.example_data import election
[5]:
election_data = election()
election_data.head()
[5]:
district | Coderre | Bergeron | Joly | total | winner | result | district_id | geometry | |
---|---|---|---|---|---|---|---|---|---|
0 | 101-Bois-de-Liesse | 2481 | 1829 | 3024 | 7334 | Joly | plurality | 101 | MULTIPOLYGON (((-73.81877 45.51408, -73.81824 ... |
1 | 102-Cap-Saint-Jacques | 2525 | 1163 | 2675 | 6363 | Joly | plurality | 102 | MULTIPOLYGON (((-73.84617 45.48899, -73.85053 ... |
2 | 11-Sault-au-Récollet | 3348 | 2770 | 2532 | 8650 | Coderre | plurality | 11 | MULTIPOLYGON (((-73.63632 45.57592, -73.63628 ... |
3 | 111-Mile-End | 1734 | 4782 | 2514 | 9030 | Bergeron | majority | 111 | POLYGON ((-73.57920 45.52801, -73.58151 45.525... |
4 | 112-DeLorimier | 1770 | 5933 | 3044 | 10747 | Bergeron | majority | 112 | POLYGON ((-73.56556 45.52136, -73.57406 45.525... |
The plotly express choropleth_mapbox
function is great, but it requires a lot of arguments to be set to work properly.
[6]:
import plotly.express as px
fig = px.choropleth_mapbox(
election_data,
geojson=election_data.__geo_interface__,
color="Bergeron",
locations="district",
featureidkey="properties.district",
center={"lat": 45.5517, "lon": -73.7073},
zoom=9,
)
plotly.graph_objects.FigureWidget(fig)
We can use the mapped
interface to create substantially the same plot with just a single argument: the column to use to colorize the choropleth.
[7]:
election_data.plotly_choropleth(color="Bergeron")
-
GeoDataFrame.
plotly_choropleth
(color=None, *, zoom='auto', mapbox_style=None, margins=0, figuretype=None, fig=None, center=None, opacity=1.0, text=None, **kwargs)¶ Make a choropleth point map in a plotly FigureWidget.
Parameters: - gdf (geopandas.GeoDataFrame) – The areas to plot.
- zoom (‘auto’ or int or float) – Sets the initial zoom level for the map, up to 20.
- mapbox_style (str, optional) – Sets the style for the basemap tiles. Totally free options include “carto-positron”, “stamen-terrain”, “stamen-toner”, “open-street-map”, possibly others. Options from the set {‘basic’,’streets’,’outdoors’,’light’,’dark’,’satellite’, ‘satellite-streets’} will load vector tiles from MapBox, which requires a token to be set. This defaults to “carto-positron” if no mapbox token is set, or ‘basic’ if a token is available.
- margins (int, optional) – Set margins on the figure.
- figuretype (class, optional) – Which plotly figure class to use, defaults to plotly.go.FigureWidget.
- fig (plotly.go.Figure or plotly.go.FigureWidget) – An existing figure, to which the new trace(s) will be appended.
- color (str or int or Series or array-like) – Either a name of a column in gdf, or a pandas Series or array_like object. Values are used to assign color to markers.
- hover_name (str or int or Series or array-like) – Either a name of a column in gdf, or a pandas Series or array_like object. Values appear in bold in the hover tooltip.
- hover_data (list of str or int, or Series or array-like) – Either names of columns in gdf, or pandas Series, or array_like objects Values appear as extra data in the hover tooltip.
- opacity (float, default 0.5) – Value between 0 and 1. Sets the opacity for markers.
- color_discrete_sequence (list of str) – Strings should define valid CSS-colors. When color is set and the values in the corresponding column are not numeric, values in that column are assigned colors by cycling through color_discrete_sequence in the order described in category_orders, unless the value of color is a key in color_discrete_map. Various useful color sequences are available in the plotly.express.colors submodules, specifically plotly.express.colors.qualitative.
- color_discrete_map (dict with str keys and str values (default {})) – String values should define valid CSS-colors Used to override color_discrete_sequence to assign a specific colors to marks corresponding with specific values. Keys in color_discrete_map should be values in the column denoted by color.
- color_continuous_scale (list of str) – Strings should define valid CSS-colors This list is used to build a continuous color scale when the column denoted by color contains numeric data. Various useful color scales are available in the plotly.express.colors submodules, specifically plotly.express.colors.sequential, plotly.express.colors.diverging and plotly.express.colors.cyclical.
- range_color (list of two numbers) – If provided, overrides auto-scaling on the continuous color scale.
- color_continuous_midpoint (number (default None)) – If set, computes the bounds of the continuous color scale to have the desired midpoint. Setting this value is recommended when using plotly.express.colors.diverging color scales as the inputs to color_continuous_scale.
- width (int (default None)) – The figure width in pixels.
- height (int (default 600)) – The figure height in pixels.
- **kwargs – Other keyword arguments are passed through to the plotly.express.choropleth_mapbox constructor, allowing substantial further customization of the resulting figure.
Returns: Return type: plotly.go.FigureWidget