Skip to content

Commit 80f71f2

Browse files
norms in decorated labels
1 parent 9183085 commit 80f71f2

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

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

+12-6
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):

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)