Skip to content

Commit deaa889

Browse files
Merge pull request #2057 from plotly/choropleth_geojson
categorical choropleths
2 parents 5ac8f55 + e5d4623 commit deaa889

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

Diff for: packages/python/plotly/plotly/express/_chart_types.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,8 @@ def choropleth(
843843
lon=None,
844844
locations=None,
845845
locationmode=None,
846+
geojson=None,
847+
featureidkey=None,
846848
color=None,
847849
hover_name=None,
848850
hover_data=None,
@@ -851,6 +853,8 @@ def choropleth(
851853
animation_group=None,
852854
category_orders={},
853855
labels={},
856+
color_discrete_sequence=None,
857+
color_discrete_map={},
854858
color_continuous_scale=None,
855859
range_color=None,
856860
color_continuous_midpoint=None,
@@ -869,7 +873,13 @@ def choropleth(
869873
return make_figure(
870874
args=locals(),
871875
constructor=go.Choropleth,
872-
trace_patch=dict(locationmode=locationmode),
876+
trace_patch=dict(
877+
locationmode=locationmode,
878+
featureidkey=featureidkey,
879+
geojson=geojson
880+
if not hasattr(geojson, "__geo_interface__") # for geopandas
881+
else geojson.__geo_interface__,
882+
),
873883
)
874884

875885

@@ -1006,6 +1016,7 @@ def scatter_mapbox(
10061016
def choropleth_mapbox(
10071017
data_frame=None,
10081018
geojson=None,
1019+
featureidkey=None,
10091020
locations=None,
10101021
color=None,
10111022
hover_name=None,
@@ -1015,6 +1026,8 @@ def choropleth_mapbox(
10151026
animation_group=None,
10161027
category_orders={},
10171028
labels={},
1029+
color_discrete_sequence=None,
1030+
color_discrete_map={},
10181031
color_continuous_scale=None,
10191032
range_color=None,
10201033
color_continuous_midpoint=None,
@@ -1035,9 +1048,10 @@ def choropleth_mapbox(
10351048
args=locals(),
10361049
constructor=go.Choroplethmapbox,
10371050
trace_patch=dict(
1051+
featureidkey=featureidkey,
10381052
geojson=geojson
1039-
if not hasattr(geojson, "__geo_interface__")
1040-
else geojson.__geo_interface__
1053+
if not hasattr(geojson, "__geo_interface__") # for geopandas
1054+
else geojson.__geo_interface__,
10411055
),
10421056
)
10431057

Diff for: packages/python/plotly/plotly/express/_core.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1250,8 +1250,9 @@ def make_figure(args, constructor, trace_patch={}, layout_patch={}):
12501250
if val not in m.val_map:
12511251
m.val_map[val] = m.sequence[len(m.val_map) % len(m.sequence)]
12521252
try:
1253-
m.updater(trace, m.val_map[val])
1253+
m.updater(trace, m.val_map[val]) # covers most cases
12541254
except ValueError:
1255+
# this catches some odd cases like marginals
12551256
if (
12561257
trace_spec != trace_specs[0]
12571258
and trace_spec.constructor in [go.Violin, go.Box, go.Histogram]
@@ -1264,6 +1265,16 @@ def make_figure(args, constructor, trace_patch={}, layout_patch={}):
12641265
and m.variable == "color"
12651266
):
12661267
trace.update(marker=dict(color=m.val_map[val]))
1268+
elif (
1269+
trace_spec.constructor in [go.Choropleth, go.Choroplethmapbox]
1270+
and m.variable == "color"
1271+
):
1272+
trace.update(
1273+
z=[1] * len(group),
1274+
colorscale=[m.val_map[val]] * 2,
1275+
showscale=False,
1276+
showlegend=True,
1277+
)
12671278
else:
12681279
raise
12691280

Diff for: packages/python/plotly/plotly/express/_doc.py

+5
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,11 @@
471471
"GeoJSON-formatted dict",
472472
"Must contain a Polygon feature collection, with IDs, which are references from `locations`.",
473473
],
474+
featureidkey=[
475+
"str (default: `'id'`)",
476+
"Path to field in GeoJSON feature object with which to match the values passed in to `locations`."
477+
"The most common alternative to the default is of the form `'properties.<key>`.",
478+
],
474479
cumulative=[
475480
"boolean (default `False`)",
476481
"If `True`, histogram values are cumulative.",

0 commit comments

Comments
 (0)