Skip to content

Commit 68baeaa

Browse files
categorical choropleths
1 parent ec09285 commit 68baeaa

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

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

+16-2
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,8 @@ def choropleth(
840840
lon=None,
841841
locations=None,
842842
locationmode=None,
843+
geojson=None,
844+
featureidkey=None,
843845
color=None,
844846
hover_name=None,
845847
hover_data=None,
@@ -848,6 +850,8 @@ def choropleth(
848850
animation_group=None,
849851
category_orders={},
850852
labels={},
853+
color_discrete_sequence=None,
854+
color_discrete_map={},
851855
color_continuous_scale=None,
852856
range_color=None,
853857
color_continuous_midpoint=None,
@@ -866,7 +870,13 @@ def choropleth(
866870
return make_figure(
867871
args=locals(),
868872
constructor=go.Choropleth,
869-
trace_patch=dict(locationmode=locationmode),
873+
trace_patch=dict(
874+
locationmode=locationmode,
875+
featureidkey=featureidkey,
876+
geojson=geojson
877+
if not hasattr(geojson, "__geo_interface__")
878+
else geojson.__geo_interface__,
879+
),
870880
)
871881

872882

@@ -1003,6 +1013,7 @@ def scatter_mapbox(
10031013
def choropleth_mapbox(
10041014
data_frame=None,
10051015
geojson=None,
1016+
featureidkey=None,
10061017
locations=None,
10071018
color=None,
10081019
hover_name=None,
@@ -1012,6 +1023,8 @@ def choropleth_mapbox(
10121023
animation_group=None,
10131024
category_orders={},
10141025
labels={},
1026+
color_discrete_sequence=None,
1027+
color_discrete_map={},
10151028
color_continuous_scale=None,
10161029
range_color=None,
10171030
color_continuous_midpoint=None,
@@ -1032,9 +1045,10 @@ def choropleth_mapbox(
10321045
args=locals(),
10331046
constructor=go.Choroplethmapbox,
10341047
trace_patch=dict(
1048+
featureidkey=featureidkey,
10351049
geojson=geojson
10361050
if not hasattr(geojson, "__geo_interface__")
1037-
else geojson.__geo_interface__
1051+
else geojson.__geo_interface__,
10381052
),
10391053
)
10401054

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)