Skip to content

fixing hover_data and hover_name bugs in path API #2524

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 3 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1432,23 +1432,12 @@ def process_dataframe_hierarchy(args):
_check_dataframe_all_leaves(df[path[::-1]])
discrete_color = False

if args["color"] and args["color"] in path:
series_to_copy = df[args["color"]]
new_col_name = args["color"] + "additional_col_for_color"
path = [new_col_name if x == args["color"] else x for x in path]
df[new_col_name] = series_to_copy
if args["hover_data"]:
for col_name in args["hover_data"]:
if col_name == args["color"]:
series_to_copy = df[col_name]
new_col_name = str(args["color"]) + "additional_col_for_hover"
df[new_col_name] = series_to_copy
args["color"] = new_col_name
elif col_name in path:
series_to_copy = df[col_name]
new_col_name = col_name + "additional_col_for_hover"
path = [new_col_name if x == col_name else x for x in path]
df[new_col_name] = series_to_copy
new_path = []
for col_name in path:
new_col_name = col_name + "_path_copy"
new_path.append(new_col_name)
df[new_col_name] = df[col_name]
path = new_path
# ------------ Define aggregation functions --------------------------------

def aggfunc_discrete(x):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,23 @@ def test_sunburst_treemap_with_path_and_hover():
)
assert "smoker" in fig.data[0].hovertemplate

df = px.data.gapminder().query("year == 2007")
fig = px.sunburst(
df, path=["continent", "country"], color="lifeExp", hover_data=df.columns
)
assert fig.layout.coloraxis.colorbar.title.text == "lifeExp"
Copy link
Contributor Author

@nicolaskruchten nicolaskruchten Jun 1, 2020

Choose a reason for hiding this comment

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

on master this appears as lifeExpadditional_col_for_hover because we rewrite args["color"]


df = px.data.tips()
fig = px.sunburst(df, path=["sex", "day", "time", "smoker"], hover_name="smoker")
Copy link
Contributor Author

@nicolaskruchten nicolaskruchten Jun 1, 2020

Choose a reason for hiding this comment

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

this actually errors out on master (because smoker is in path but isn't accounted for in hover_name)

assert "smoker" not in fig.data[0].hovertemplate # represented as '%{hovertext}'
assert "%{hovertext}" in fig.data[0].hovertemplate # represented as '%{hovertext}'

df = px.data.tips()
fig = px.sunburst(df, path=["sex", "day", "time", "smoker"], custom_data=["smoker"])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this also errors out on master (because smoker is in path but isn't accounted for in custom_data)

assert fig.data[0].customdata[0][0] in ["Yes", "No"]
assert "smoker" not in fig.data[0].hovertemplate
assert "%{hovertext}" not in fig.data[0].hovertemplate


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