From d7b2cc961b5718e224c87d01b4db69fba27c7e33 Mon Sep 17 00:00:00 2001 From: Emmanuelle Gouillart Date: Fri, 24 Jan 2020 09:21:43 -0500 Subject: [PATCH 1/5] removed booleans from continuous color types --- packages/python/plotly/plotly/express/_core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index e94a79d3954..4f3028297a1 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -1188,7 +1188,7 @@ def infer_config(args, constructor, trace_patch): else: if ( args["color"] - and args["data_frame"][args["color"]].dtype.kind in "bifc" + and args["data_frame"][args["color"]].dtype.kind in "ifc" ): attrs.append("color") args["color_is_continuous"] = True From 08fbda142697c8df6eceabe85c562cec563de8fa Mon Sep 17 00:00:00 2001 From: Emmanuelle Gouillart Date: Fri, 24 Jan 2020 09:35:10 -0500 Subject: [PATCH 2/5] bifc -> ifc --- packages/python/plotly/plotly/express/_core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index 4f3028297a1..fed8a7a9a0d 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -177,7 +177,7 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref): if ((not v) or (name in v)) and ( trace_spec.constructor != go.Parcoords - or args["data_frame"][name].dtype.kind in "bifc" + or args["data_frame"][name].dtype.kind in "ifc" ) and ( trace_spec.constructor != go.Parcats @@ -1094,7 +1094,7 @@ def aggfunc_discrete(x): agg_f[count_colname] = "sum" if args["color"]: - if df[args["color"]].dtype.kind not in "bifc": + if df[args["color"]].dtype.kind not in "ifc": aggfunc_color = aggfunc_discrete discrete_color = True elif not aggfunc_color: From e1848c995fcc98d2a4a75971f06569275b6b6c85 Mon Sep 17 00:00:00 2001 From: Emmanuelle Gouillart Date: Tue, 10 Mar 2020 14:44:35 -0400 Subject: [PATCH 3/5] fix bug --- packages/python/plotly/plotly/express/_core.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index 169b9933795..0182d24916e 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -1168,7 +1168,12 @@ def aggfunc_continuous(x): df_all_trees = df_all_trees.append(df_tree, ignore_index=True) if args["color"] and discrete_color: - df_all_trees = df_all_trees.sort_values(by=args["color"]) + if "sort_color_if_discrete_color" not in df_all_trees.columns: + df_all_trees["sort_color_if_discrete_color"] = df[args["color"]].astype(str) + sort_col = "sort_color_if_discrete_color" + else: + sort_col = args["color"] + df_all_trees = df_all_trees.sort_values(by=sort_col) # Now modify arguments args["data_frame"] = df_all_trees From 867b8365f57aab5b9e0fd2ef2b1e5b4c999063cd Mon Sep 17 00:00:00 2001 From: Emmanuelle Gouillart Date: Wed, 11 Mar 2020 10:50:17 -0400 Subject: [PATCH 4/5] added tests and better handling of corner case for naming additional column --- .../python/plotly/plotly/express/_core.py | 12 ++++----- test/percy/plotly-express.py | 27 +++++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index 0182d24916e..de2af43131d 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -1167,13 +1167,13 @@ def aggfunc_continuous(x): df_tree[cols] = dfg[cols] df_all_trees = df_all_trees.append(df_tree, ignore_index=True) + # we want to make sure than (?) is the first color of the sequence if args["color"] and discrete_color: - if "sort_color_if_discrete_color" not in df_all_trees.columns: - df_all_trees["sort_color_if_discrete_color"] = df[args["color"]].astype(str) - sort_col = "sort_color_if_discrete_color" - else: - sort_col = args["color"] - df_all_trees = df_all_trees.sort_values(by=sort_col) + sort_col_name = "sort_color_if_discrete_color" + while sort_col_name in df_all_trees.columns: + sort_col_name += "0" + df_all_trees[sort_col_name] = df[args["color"]].astype(str) + df_all_trees = df_all_trees.sort_values(by=sort_col_name) # Now modify arguments args["data_frame"] = df_all_trees diff --git a/test/percy/plotly-express.py b/test/percy/plotly-express.py index a34fc55e8cb..5e8622900eb 100644 --- a/test/percy/plotly-express.py +++ b/test/percy/plotly-express.py @@ -550,3 +550,30 @@ y=["first", "second", "first", "second"], x=[3, 1, 4, 2], color=["A", "A", "B", "B"] ) fig.write_html(os.path.join(dir_name, "funnel.html"), auto_play=False) + +import plotly.express as px +fig = px.scatter(x=[1, 2, 1, 2], y=[4, 3, 2, 4], color=[True, True, False, False]) +fig.write_html(os.path.join(dir_name, "scatter_bool_color.html"), auto_play=False) + + +import plotly.express as px +fig = px.pie(values=[1, 2, 3, 4], color=[True, False, True, False]) +fig.write_html(os.path.join(dir_name, "pie_bool_color.html"), auto_play=False) + +import plotly.express as px +import numpy as np +df = px.data.gapminder().query("year == 2007") +np.random.seed(0) +df["color"] = np.random.choice([True, False], len(df)) +fig = px.choropleth(df, locations='iso_alpha', color='color') +fig.write_html(os.path.join(dir_name, "choropleth_bool_color.html"), auto_play=False) + +import plotly.express as px +df = px.data.iris() +df["is_setosa"] = df['species'] == 'setosa' +fig = px.density_contour(df, x="sepal_width", y="sepal_length", color='is_setosa') +fig.write_html(os.path.join(dir_name, "density_contour_bool_color.html"), auto_play=False) + +import plotly.express as px +fig = px.sunburst(path=[["yes", "no", "no"],["yes", "no", "a"]], color=[True, False, True]) +fig.write_html(os.path.join(dir_name, "sunburst_bool_color.html"), auto_play=False) From 1be301651bd3b3ea19112fe8b15b2c4eb39b8ba2 Mon Sep 17 00:00:00 2001 From: Emmanuelle Gouillart Date: Wed, 11 Mar 2020 11:24:30 -0400 Subject: [PATCH 5/5] blacken --- test/percy/plotly-express.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/test/percy/plotly-express.py b/test/percy/plotly-express.py index 5e8622900eb..0a1b1064c50 100644 --- a/test/percy/plotly-express.py +++ b/test/percy/plotly-express.py @@ -552,28 +552,37 @@ fig.write_html(os.path.join(dir_name, "funnel.html"), auto_play=False) import plotly.express as px + fig = px.scatter(x=[1, 2, 1, 2], y=[4, 3, 2, 4], color=[True, True, False, False]) fig.write_html(os.path.join(dir_name, "scatter_bool_color.html"), auto_play=False) import plotly.express as px + fig = px.pie(values=[1, 2, 3, 4], color=[True, False, True, False]) fig.write_html(os.path.join(dir_name, "pie_bool_color.html"), auto_play=False) import plotly.express as px import numpy as np + df = px.data.gapminder().query("year == 2007") np.random.seed(0) df["color"] = np.random.choice([True, False], len(df)) -fig = px.choropleth(df, locations='iso_alpha', color='color') +fig = px.choropleth(df, locations="iso_alpha", color="color") fig.write_html(os.path.join(dir_name, "choropleth_bool_color.html"), auto_play=False) import plotly.express as px + df = px.data.iris() -df["is_setosa"] = df['species'] == 'setosa' -fig = px.density_contour(df, x="sepal_width", y="sepal_length", color='is_setosa') -fig.write_html(os.path.join(dir_name, "density_contour_bool_color.html"), auto_play=False) +df["is_setosa"] = df["species"] == "setosa" +fig = px.density_contour(df, x="sepal_width", y="sepal_length", color="is_setosa") +fig.write_html( + os.path.join(dir_name, "density_contour_bool_color.html"), auto_play=False +) import plotly.express as px -fig = px.sunburst(path=[["yes", "no", "no"],["yes", "no", "a"]], color=[True, False, True]) + +fig = px.sunburst( + path=[["yes", "no", "no"], ["yes", "no", "a"]], color=[True, False, True] +) fig.write_html(os.path.join(dir_name, "sunburst_bool_color.html"), auto_play=False)