Skip to content

Commit c4fef05

Browse files
Merge pull request #2989 from plotly/clarify
Fix histogram labels
2 parents ce9eb96 + 80f71f2 commit c4fef05

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

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

+21-20
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,22 @@ def _is_continuous(df, col_name):
149149
def get_decorated_label(args, column, role):
150150
label = get_label(args, column)
151151
if "histfunc" in args and (
152-
(role == "x" and "orientation" in args and args["orientation"] == "h")
152+
(role == "z")
153+
or (role == "x" and "orientation" in args and args["orientation"] == "h")
153154
or (role == "y" and "orientation" in args and args["orientation"] == "v")
154-
or (role == "z")
155155
):
156156
if label:
157-
return "%s of %s" % (args["histfunc"] or "count", label)
157+
label = "%s of %s" % (args["histfunc"] or "count", label)
158158
else:
159-
return "count"
160-
else:
161-
return label
159+
label = "count"
160+
161+
if "histnorm" in args and args["histnorm"] is not None:
162+
label = "%s of %s" % (args["histnorm"], label)
163+
164+
if "barnorm" in args and args["barnorm"] is not None:
165+
label = "%s (normalized as %s)" % (label, args["barnorm"])
166+
167+
return label
162168

163169

164170
def make_mapping(args, variable):
@@ -264,14 +270,7 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
264270
mapping_labels["%{xaxis.title.text}"] = "%{x}"
265271
mapping_labels["%{yaxis.title.text}"] = "%{y}"
266272

267-
elif (
268-
attr_value is not None
269-
or (trace_spec.constructor == go.Histogram and attr_name in ["x", "y"])
270-
or (
271-
trace_spec.constructor in [go.Histogram2d, go.Histogram2dContour]
272-
and attr_name == "z"
273-
)
274-
):
273+
elif attr_value is not None:
275274
if attr_name == "size":
276275
if "marker" not in trace_patch:
277276
trace_patch["marker"] = dict()
@@ -464,13 +463,15 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
464463
else:
465464
trace_patch[attr_name] = trace_data[attr_value]
466465
else:
467-
if attr_value:
468-
trace_patch[attr_name] = trace_data[attr_value]
466+
trace_patch[attr_name] = trace_data[attr_value]
469467
mapping_labels[attr_label] = "%%{%s}" % attr_name
470-
if trace_spec.constructor not in [
471-
go.Parcoords,
472-
go.Parcats,
473-
]:
468+
elif (trace_spec.constructor == go.Histogram and attr_name in ["x", "y"]) or (
469+
trace_spec.constructor in [go.Histogram2d, go.Histogram2dContour]
470+
and attr_name == "z"
471+
):
472+
# ensure that stuff like "count" gets into the hoverlabel
473+
mapping_labels[attr_label] = "%%{%s}" % attr_name
474+
if trace_spec.constructor not in [go.Parcoords, go.Parcats]:
474475
# Modify mapping_labels according to hover_data keys
475476
# if hover_data is a dict
476477
mapping_labels_copy = OrderedDict(mapping_labels)

Diff for: packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def test_sunburst_treemap_column_parent():
267267
df = pd.DataFrame(dict(id=vendors, sectors=sectors, parent=regions, values=values,))
268268
path = ["parent", "sectors", "id"]
269269
# One column of the path is a reserved name - this is ok and should not raise
270-
fig = px.sunburst(df, path=path, values="values")
270+
px.sunburst(df, path=path, values="values")
271271

272272

273273
def test_sunburst_treemap_with_path_non_rectangular():
@@ -379,6 +379,31 @@ def test_parcats_dimensions_max():
379379
assert [d.label for d in fig.data[0].dimensions] == ["sex", "smoker", "day", "size"]
380380

381381

382+
def test_histfunc_hoverlabels():
383+
df = px.data.tips()
384+
fig = px.histogram(df, x="total_bill")
385+
label = "count"
386+
assert fig.layout.yaxis.title.text == label
387+
assert label + "=" in fig.data[0].hovertemplate
388+
389+
fig = px.histogram(df, x="total_bill", y="tip")
390+
label = "sum of tip"
391+
assert fig.layout.yaxis.title.text == label
392+
assert label + "=" in fig.data[0].hovertemplate
393+
394+
fig = px.histogram(
395+
df,
396+
x="total_bill",
397+
y="tip",
398+
histfunc="min",
399+
histnorm="probability",
400+
barnorm="percent",
401+
)
402+
label = "probability of min of tip (normalized as percent)"
403+
assert fig.layout.yaxis.title.text == label
404+
assert label + "=" in fig.data[0].hovertemplate
405+
406+
382407
def test_timeline():
383408
df = pd.DataFrame(
384409
[

0 commit comments

Comments
 (0)