diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index 9d586415633..de2af43131d 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -188,7 +188,7 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): if ((not attr_value) or (name in attr_value)) 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 @@ -1123,7 +1123,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: @@ -1167,8 +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: - df_all_trees = df_all_trees.sort_values(by=args["color"]) + 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 @@ -1222,7 +1227,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 diff --git a/test/percy/plotly-express.py b/test/percy/plotly-express.py index a34fc55e8cb..0a1b1064c50 100644 --- a/test/percy/plotly-express.py +++ b/test/percy/plotly-express.py @@ -550,3 +550,39 @@ 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)