Skip to content

Commit 1947333

Browse files
committed
first draft of adding pie, sunburst, funnel and funnelarea
1 parent 3ca829c commit 1947333

File tree

4 files changed

+174
-4
lines changed

4 files changed

+174
-4
lines changed

packages/python/plotly/plotly/express/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
choropleth,
4040
density_contour,
4141
density_heatmap,
42+
pie,
43+
sunburst,
44+
funnel,
45+
funnel_area,
4246
)
4347

4448
from ._imshow import imshow
@@ -77,6 +81,10 @@
7781
"strip",
7882
"histogram",
7983
"choropleth",
84+
"pie",
85+
"sunburst",
86+
"funnel"
87+
"funnel_area"
8088
"imshow",
8189
"data",
8290
"colors",

packages/python/plotly/plotly/express/_chart_types.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,3 +1115,118 @@ def parallel_categories(
11151115

11161116

11171117
parallel_categories.__doc__ = make_docstring(parallel_categories)
1118+
1119+
1120+
def pie(
1121+
data_frame=None,
1122+
text=None,
1123+
values=None,
1124+
hover_name=None,
1125+
hover_data=None,
1126+
custom_data=None,
1127+
labels={},
1128+
title=None,
1129+
template=None,
1130+
width=None,
1131+
height=None,
1132+
opacity=None,
1133+
):
1134+
"""
1135+
In a pie plot, each row of `data_frame` is represented as a sector of a pie.
1136+
"""
1137+
return make_figure(
1138+
args=locals(),
1139+
constructor=go.Pie,
1140+
)
1141+
1142+
1143+
pie.__doc__ = make_docstring(pie)
1144+
1145+
1146+
def sunburst(
1147+
data_frame=None,
1148+
text=None,
1149+
values=None,
1150+
parents=None,
1151+
ids=None,
1152+
hover_name=None,
1153+
hover_data=None,
1154+
custom_data=None,
1155+
labels={},
1156+
title=None,
1157+
template=None,
1158+
width=None,
1159+
height=None,
1160+
branchvalues=None,
1161+
maxdepth=None,
1162+
):
1163+
"""
1164+
In a pie plot, each row of `data_frame` is represented as a sector of a pie.
1165+
"""
1166+
return make_figure(
1167+
args=locals(),
1168+
constructor=go.Sunburst,
1169+
trace_patch=dict(branchvalues=branchvalues, maxdepth=maxdepth)
1170+
)
1171+
1172+
1173+
sunburst.__doc__ = make_docstring(sunburst)
1174+
1175+
1176+
1177+
def funnel(
1178+
data_frame=None,
1179+
x=None,
1180+
y=None,
1181+
color=None,
1182+
color_discrete_sequence=None,
1183+
color_discrete_map={},
1184+
orientation=None,
1185+
textinfo=None,
1186+
hover_name=None,
1187+
hover_data=None,
1188+
custom_data=None,
1189+
labels={},
1190+
title=None,
1191+
template=None,
1192+
width=None,
1193+
height=None,
1194+
opacity=None,
1195+
):
1196+
"""
1197+
In a funnel plot, each row of `data_frame` is represented as a rectangular sector of a funnel.
1198+
"""
1199+
return make_figure(
1200+
args=locals(),
1201+
constructor=go.Funnel,
1202+
trace_patch=dict(opacity=opacity, orientation=orientation, textinfo=textinfo),
1203+
)
1204+
1205+
1206+
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)."]))
1207+
1208+
1209+
def funnel_area(
1210+
data_frame=None,
1211+
values=None,
1212+
text=None,
1213+
textinfo=None,
1214+
hover_name=None,
1215+
hover_data=None,
1216+
custom_data=None,
1217+
labels={},
1218+
title=None,
1219+
template=None,
1220+
width=None,
1221+
height=None,
1222+
):
1223+
"""
1224+
In a funnel area plot, each row of `data_frame` is represented as a trapezoidal sector of a funnel.
1225+
"""
1226+
return make_figure(
1227+
args=locals(),
1228+
constructor=go.Funnelarea,
1229+
)
1230+
1231+
1232+
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)."]))

packages/python/plotly/plotly/express/_core.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,25 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
305305
elif k == "locations":
306306
result[k] = g[v]
307307
mapping_labels[v_label] = "%{location}"
308+
elif k == "values":
309+
result[k] = g[v]
310+
mapping_labels["value"] = "%{value}"
311+
elif k == "parents":
312+
result[k] = g[v]
313+
mapping_labels["parent"] = "%{parent}"
314+
elif k == "ids":
315+
result[k] = g[v]
316+
mapping_labels["id"] = "%{id}"
317+
elif k == "text":
318+
if trace_spec.constructor in [go.Sunburst, go.Treemap]:
319+
result["labels"] = g[v]
320+
else:
321+
result[k] = g[v]
308322
else:
309323
if v:
310324
result[k] = g[v]
311325
mapping_labels[v_label] = "%%{%s}" % k
312-
if trace_spec.constructor not in [go.Parcoords, go.Parcats]:
326+
if trace_spec.constructor not in [go.Parcoords, go.Parcats, go.Sunburst, go.Treemap]:
313327
hover_lines = [k + "=" + v for k, v in mapping_labels.items()]
314328
result["hovertemplate"] = hover_header + "<br>".join(hover_lines)
315329
return result, fit_results
@@ -956,6 +970,7 @@ def infer_config(args, constructor, trace_patch):
956970
attrables = (
957971
["x", "y", "z", "a", "b", "c", "r", "theta", "size", "dimensions"]
958972
+ ["custom_data", "hover_name", "hover_data", "text"]
973+
+ ["values", "parents", "ids"]
959974
+ ["error_x", "error_x_minus"]
960975
+ ["error_y", "error_y_minus", "error_z", "error_z_minus"]
961976
+ ["lat", "lon", "locations", "animation_group"]
@@ -997,6 +1012,8 @@ def infer_config(args, constructor, trace_patch):
9971012
grouped_attrs.append("marker.color")
9981013

9991014
show_colorbar = bool("color" in attrs and args["color"])
1015+
else:
1016+
show_colorbar=False
10001017

10011018
# Compute line_dash grouping attribute
10021019
if "line_dash" in args:
@@ -1148,6 +1165,7 @@ def make_figure(args, constructor, trace_patch={}, layout_patch={}):
11481165
go.Parcoords,
11491166
go.Choropleth,
11501167
go.Histogram2d,
1168+
go.Sunburst,
11511169
]:
11521170
trace.update(
11531171
legendgroup=trace_name,

packages/python/plotly/plotly/express/_doc.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@
6666
colref_desc,
6767
"Values from this column or array_like are used to position marks along the angular axis in polar coordinates.",
6868
],
69+
values=[
70+
colref_type,
71+
colref_desc,
72+
"Values from this column or array_like are used to set values associated to sectors."
73+
],
74+
parents=[
75+
colref_type,
76+
colref_desc,
77+
"Values from this column or array_like are used to set values associated to sectors."
78+
],
79+
ids=[
80+
colref_type,
81+
colref_desc,
82+
"Values from this column or array_like are used to set values associated to sectors."
83+
],
6984
lat=[
7085
colref_type,
7186
colref_desc,
@@ -442,21 +457,35 @@
442457
nbins=["int", "Positive integer.", "Sets the number of bins."],
443458
nbinsx=["int", "Positive integer.", "Sets the number of bins along the x axis."],
444459
nbinsy=["int", "Positive integer.", "Sets the number of bins along the y axis."],
460+
branchvalues=["str", "'total' or 'remainder'",
461+
"Determines how the items in `values` are summed. When"
462+
"set to 'total', items in `values` are taken to be value"
463+
"of all its descendants. When set to 'remainder', items"
464+
"in `values` corresponding to the root and the branches"
465+
":sectors are taken to be the extra part not part of the"
466+
"sum of the values at their leaves."],
467+
maxdepth=["int", "Positive integer",
468+
"Sets the number of rendered sectors from any given `level`. Set `maxdepth` to -1 to render all the"
469+
"levels in the hierarchy."]
445470
)
446471

447472

448-
def make_docstring(fn):
473+
def make_docstring(fn, override_dict={}):
449474
tw = TextWrapper(width=77, initial_indent=" ", subsequent_indent=" ")
450475
result = (fn.__doc__ or "") + "\nParameters\n----------\n"
451476
for param in inspect.getargspec(fn)[0]:
452-
param_desc_list = docs[param][1:]
477+
if override_dict.get(param):
478+
param_doc = override_dict[param]
479+
else:
480+
param_doc = docs[param]
481+
param_desc_list = param_doc[1:]
453482
param_desc = (
454483
tw.fill(" ".join(param_desc_list or ""))
455484
if param in docs
456485
else "(documentation missing from map)"
457486
)
458487

459-
param_type = docs[param][0]
488+
param_type = param_doc[0]
460489
result += "%s: %s\n%s\n" % (param, param_type, param_desc)
461490
result += "\nReturns\n-------\n"
462491
result += " A `Figure` object."

0 commit comments

Comments
 (0)