Skip to content

adding pie, treemap, sunburst, funnel and funnelarea to px #1909

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

Merged
merged 15 commits into from
Nov 28, 2019
38 changes: 6 additions & 32 deletions packages/python/plotly/plotly/express/_chart_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,6 @@ def pie(
color=None,
color_discrete_sequence=None,
color_discrete_map={},
textinfo=None,
hover_name=None,
hover_data=None,
custom_data=None,
Expand All @@ -1146,15 +1145,14 @@ def pie(
return make_figure(
args=locals(),
constructor=go.Pie,
trace_patch=dict(showlegend=True, hole=hole),
trace_patch=dict(showlegend=(names is not None), hole=hole),
layout_patch=layout_patch,
)


pie.__doc__ = make_docstring(
pie,
override_dict=dict(
textinfo=["str", "Determines which trace information appear on the graph.",],
hole=[
"float",
"Sets the fraction of the radius to cut out of the pie."
Expand Down Expand Up @@ -1230,7 +1228,7 @@ def treemap(
maxdepth=None,
):
"""
A treemap plot represents hierarchial data as nested rectangular sectors.
A treemap plot represents hierarchial data as nested rectangular sectors.
"""
if color_discrete_sequence is not None:
layout_patch = {"treemapcolorway": color_discrete_sequence}
Expand Down Expand Up @@ -1259,22 +1257,14 @@ def funnel(
hover_data=None,
custom_data=None,
text=None,
error_x=None,
error_x_minus=None,
error_y=None,
error_y_minus=None,
animation_frame=None,
animation_group=None,
category_orders={},
labels={},
color_discrete_sequence=None,
color_discrete_map={},
color_continuous_scale=None,
range_color=None,
color_continuous_midpoint=None,
opacity=None,
orientation="h",
barmode="relative",
log_x=False,
log_y=False,
range_x=None,
Expand All @@ -1294,15 +1284,7 @@ def funnel(
)


funnel.__doc__ = make_docstring(
funnel,
override_dict=dict(
textinfo=[
"str",
"Determines which trace information appear on the graph. In the case of having multiple funnels, percentages & totals are computed separately (per trace).",
]
),
)
funnel.__doc__ = make_docstring(funnel)


def funnel_area(
Expand All @@ -1312,7 +1294,6 @@ def funnel_area(
color=None,
color_discrete_sequence=None,
color_discrete_map={},
textinfo=None,
hover_name=None,
hover_data=None,
custom_data=None,
Expand All @@ -1333,17 +1314,10 @@ def funnel_area(
return make_figure(
args=locals(),
constructor=go.Funnelarea,
trace_patch=dict(showlegend=True),
trace_patch=dict(showlegend=(names is not None)),
layout_patch=layout_patch,
)


funnel_area.__doc__ = make_docstring(
funnel_area,
override_dict=dict(
textinfo=[
"str",
"Determines which trace information appear on the graph. In the case of having multiple funnels, percentages & totals are computed separately (per trace).",
]
),
)
funnel_area.__doc__ = make_docstring(funnel_area)

53 changes: 23 additions & 30 deletions packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,6 @@ def get_trendline_results(fig):
return fig._px_trendlines


def make_color_mapping(cat_list, discrete_colorscale):
mapping = {}
colors = []
taken = 0
length = len(discrete_colorscale)
for cat in cat_list:
if mapping.get(cat) is None:
mapping[cat] = discrete_colorscale[taken % length]
taken += 1
colors.append(mapping[cat])
return colors


Mapping = namedtuple(
"Mapping",
[
Expand Down Expand Up @@ -304,26 +291,28 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
result["z"] = g[v]
result["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{z}"
elif trace_spec.constructor in [go.Sunburst, go.Treemap]:
colorable = "marker"
if colorable not in result:
result[colorable] = dict()
# Other if/else block
elif trace_spec.constructor in [
go.Sunburst,
go.Treemap,
go.Pie,
go.Funnelarea,
]:
if "marker" not in result:
result["marker"] = dict()

if args.get("color_is_continuous"):
result[colorable]["colors"] = g[v]
result[colorable]["coloraxis"] = "coloraxis1"
result["marker"]["colors"] = g[v]
result["marker"]["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{color}"
else:
result[colorable]["colors"] = make_color_mapping(
g[v], args["color_discrete_sequence"]
)
elif trace_spec.constructor in [go.Pie, go.Funnelarea]:
colorable = "marker"
if colorable not in result:
result[colorable] = dict()
result[colorable]["colors"] = make_color_mapping(
g[v], args["color_discrete_sequence"]
)
result["marker"]["colors"] = []
mapping = {}
for cat in g[v]:
if mapping.get(cat) is None:
mapping[cat] = args["color_discrete_sequence"][
len(mapping) % len(args["color_discrete_sequence"])
]
result["marker"]["colors"].append(mapping[cat])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting, I did not know that marker_colors could be a dictionary, I thought it had to be a list/array.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it does have to be a list, and in this case it is...?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the same code you had, just inlined :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yes I had read too fast!

else:
colorable = "marker"
if trace_spec.constructor in [go.Parcats, go.Parcoords]:
Expand Down Expand Up @@ -1064,6 +1053,10 @@ def infer_config(args, constructor, trace_patch):
grouped_attrs.append("marker.color")
else:
attrs.append("color")
if constructor in [go.Pie, go.Funnelarea]:
if args["hover_data"] is None:
args["hover_data"] = []
args["hover_data"].append(args["color"])

show_colorbar = bool(
"color" in attrs
Expand Down