Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

categorical choropleths #2057

Merged
merged 2 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions packages/python/plotly/plotly/express/_chart_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,8 @@ def choropleth(
lon=None,
locations=None,
locationmode=None,
geojson=None,
featureidkey=None,
color=None,
hover_name=None,
hover_data=None,
Expand All @@ -848,6 +850,8 @@ def choropleth(
animation_group=None,
category_orders={},
labels={},
color_discrete_sequence=None,
color_discrete_map={},
color_continuous_scale=None,
range_color=None,
color_continuous_midpoint=None,
Expand All @@ -866,7 +870,13 @@ def choropleth(
return make_figure(
args=locals(),
constructor=go.Choropleth,
trace_patch=dict(locationmode=locationmode),
trace_patch=dict(
locationmode=locationmode,
featureidkey=featureidkey,
geojson=geojson
if not hasattr(geojson, "__geo_interface__") # for geopandas
else geojson.__geo_interface__,
),
)


Expand Down Expand Up @@ -1003,6 +1013,7 @@ def scatter_mapbox(
def choropleth_mapbox(
data_frame=None,
geojson=None,
featureidkey=None,
locations=None,
color=None,
hover_name=None,
Expand All @@ -1012,6 +1023,8 @@ def choropleth_mapbox(
animation_group=None,
category_orders={},
labels={},
color_discrete_sequence=None,
color_discrete_map={},
color_continuous_scale=None,
range_color=None,
color_continuous_midpoint=None,
Expand All @@ -1032,9 +1045,10 @@ def choropleth_mapbox(
args=locals(),
constructor=go.Choroplethmapbox,
trace_patch=dict(
featureidkey=featureidkey,
geojson=geojson
if not hasattr(geojson, "__geo_interface__")
else geojson.__geo_interface__
if not hasattr(geojson, "__geo_interface__") # for geopandas
else geojson.__geo_interface__,
),
)

Expand Down
13 changes: 12 additions & 1 deletion packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1250,8 +1250,9 @@ def make_figure(args, constructor, trace_patch={}, layout_patch={}):
if val not in m.val_map:
m.val_map[val] = m.sequence[len(m.val_map) % len(m.sequence)]
try:
m.updater(trace, m.val_map[val])
m.updater(trace, m.val_map[val]) # covers most cases
except ValueError:
# this catches some odd cases like marginals
if (
trace_spec != trace_specs[0]
and trace_spec.constructor in [go.Violin, go.Box, go.Histogram]
Expand All @@ -1264,6 +1265,16 @@ def make_figure(args, constructor, trace_patch={}, layout_patch={}):
and m.variable == "color"
):
trace.update(marker=dict(color=m.val_map[val]))
elif (
trace_spec.constructor in [go.Choropleth, go.Choroplethmapbox]
and m.variable == "color"
):
trace.update(
z=[1] * len(group),
colorscale=[m.val_map[val]] * 2,
showscale=False,
showlegend=True,
)
else:
raise

Expand Down
5 changes: 5 additions & 0 deletions packages/python/plotly/plotly/express/_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@
"GeoJSON-formatted dict",
"Must contain a Polygon feature collection, with IDs, which are references from `locations`.",
],
featureidkey=[
"str (default: `'id'`)",
"Path to field in GeoJSON feature object with which to match the values passed in to `locations`."
"The most common alternative to the default is of the form `'properties.<key>`.",
],
cumulative=[
"boolean (default `False`)",
"If `True`, histogram values are cumulative.",
Expand Down