From 3d40348518bcf948351952405f0be5d710e8533b Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Mon, 1 Jun 2020 14:39:05 -0400 Subject: [PATCH 1/3] fixing hover_data and hover_name bugs in path API --- .../python/plotly/plotly/express/_core.py | 20 ++++--------------- .../test_core/test_px/test_px_functions.py | 11 ++++++++++ 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index bdb0c5dfac9..e0ec888a227 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -1432,23 +1432,11 @@ 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] + for col_name in path: + series_to_copy = df[col_name] + new_col_name = col_name + "_path_copy" + path = [new_col_name if x == col_name 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 # ------------ Define aggregation functions -------------------------------- def aggfunc_discrete(x): diff --git a/packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py b/packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py index e4ac26d28a5..1a8f69372b6 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py +++ b/packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py @@ -171,6 +171,17 @@ 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" + + df = px.data.tips() + fig = px.sunburst(df, path=["sex", "day", "time", "smoker"], hover_name="smoker") + assert "smoker" not in fig.data[0].hovertemplate # represented as '%{hovertext}' + assert "%{hovertext}" in fig.data[0].hovertemplate # represented as '%{hovertext}' + def test_sunburst_treemap_with_path_color(): vendors = ["A", "B", "C", "D", "E", "F", "G", "H"] From c21c96c22b2d43662b13a38de575b8c8f14b10ad Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Mon, 1 Jun 2020 14:43:18 -0400 Subject: [PATCH 2/3] another test --- .../plotly/tests/test_core/test_px/test_px_functions.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py b/packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py index 1a8f69372b6..9c89fcd25f0 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py +++ b/packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py @@ -182,6 +182,12 @@ def test_sunburst_treemap_with_path_and_hover(): 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"]) + 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"] From 2f401e053d93aa02cc4f81f8ce829e9844a90354 Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Wed, 3 Jun 2020 10:49:38 -0400 Subject: [PATCH 3/3] PR feedback --- packages/python/plotly/plotly/express/_core.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index e0ec888a227..df600555b14 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -1432,11 +1432,12 @@ def process_dataframe_hierarchy(args): _check_dataframe_all_leaves(df[path[::-1]]) discrete_color = False + new_path = [] for col_name in path: - series_to_copy = df[col_name] new_col_name = col_name + "_path_copy" - path = [new_col_name if x == col_name else x for x in path] - df[new_col_name] = series_to_copy + new_path.append(new_col_name) + df[new_col_name] = df[col_name] + path = new_path # ------------ Define aggregation functions -------------------------------- def aggfunc_discrete(x):