Skip to content

Commit 1f5ba2e

Browse files
fixing hover_data and hover_name bugs in path API
1 parent 7e1ee0b commit 1f5ba2e

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

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

+4-16
Original file line numberDiff line numberDiff line change
@@ -1432,23 +1432,11 @@ def process_dataframe_hierarchy(args):
14321432
_check_dataframe_all_leaves(df[path[::-1]])
14331433
discrete_color = False
14341434

1435-
if args["color"] and args["color"] in path:
1436-
series_to_copy = df[args["color"]]
1437-
new_col_name = args["color"] + "additional_col_for_color"
1438-
path = [new_col_name if x == args["color"] else x for x in path]
1435+
for col_name in path:
1436+
series_to_copy = df[col_name]
1437+
new_col_name = col_name + "_path_copy"
1438+
path = [new_col_name if x == col_name else x for x in path]
14391439
df[new_col_name] = series_to_copy
1440-
if args["hover_data"]:
1441-
for col_name in args["hover_data"]:
1442-
if col_name == args["color"]:
1443-
series_to_copy = df[col_name]
1444-
new_col_name = str(args["color"]) + "additional_col_for_hover"
1445-
df[new_col_name] = series_to_copy
1446-
args["color"] = new_col_name
1447-
elif col_name in path:
1448-
series_to_copy = df[col_name]
1449-
new_col_name = col_name + "additional_col_for_hover"
1450-
path = [new_col_name if x == col_name else x for x in path]
1451-
df[new_col_name] = series_to_copy
14521440
# ------------ Define aggregation functions --------------------------------
14531441

14541442
def aggfunc_discrete(x):

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

+11
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,17 @@ def test_sunburst_treemap_with_path_and_hover():
171171
)
172172
assert "smoker" in fig.data[0].hovertemplate
173173

174+
df = px.data.gapminder().query("year == 2007")
175+
fig = px.sunburst(
176+
df, path=["continent", "country"], color="lifeExp", hover_data=df.columns
177+
)
178+
assert fig.layout.coloraxis.colorbar.title.text == "lifeExp"
179+
180+
df = px.data.tips()
181+
fig = px.sunburst(df, path=["sex", "day", "time", "smoker"], hover_name="smoker")
182+
assert "smoker" not in fig.data[0].hovertemplate # represented as '%{hovertext}'
183+
assert "%{hovertext}" in fig.data[0].hovertemplate # represented as '%{hovertext}'
184+
174185

175186
def test_sunburst_treemap_with_path_color():
176187
vendors = ["A", "B", "C", "D", "E", "F", "G", "H"]

0 commit comments

Comments
 (0)