Skip to content

Commit 307fd73

Browse files
authored
Px bool (#2127)
* removed booleans from continuous color types * bifc -> ifc * fix bug * added tests and better handling of corner case for naming additional column * blacken
1 parent 15ab76f commit 307fd73

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
188188
if ((not attr_value) or (name in attr_value))
189189
and (
190190
trace_spec.constructor != go.Parcoords
191-
or args["data_frame"][name].dtype.kind in "bifc"
191+
or args["data_frame"][name].dtype.kind in "ifc"
192192
)
193193
and (
194194
trace_spec.constructor != go.Parcats
@@ -1123,7 +1123,7 @@ def aggfunc_discrete(x):
11231123
agg_f[count_colname] = "sum"
11241124

11251125
if args["color"]:
1126-
if df[args["color"]].dtype.kind not in "bifc":
1126+
if df[args["color"]].dtype.kind not in "ifc":
11271127
aggfunc_color = aggfunc_discrete
11281128
discrete_color = True
11291129
elif not aggfunc_color:
@@ -1167,8 +1167,13 @@ def aggfunc_continuous(x):
11671167
df_tree[cols] = dfg[cols]
11681168
df_all_trees = df_all_trees.append(df_tree, ignore_index=True)
11691169

1170+
# we want to make sure than (?) is the first color of the sequence
11701171
if args["color"] and discrete_color:
1171-
df_all_trees = df_all_trees.sort_values(by=args["color"])
1172+
sort_col_name = "sort_color_if_discrete_color"
1173+
while sort_col_name in df_all_trees.columns:
1174+
sort_col_name += "0"
1175+
df_all_trees[sort_col_name] = df[args["color"]].astype(str)
1176+
df_all_trees = df_all_trees.sort_values(by=sort_col_name)
11721177

11731178
# Now modify arguments
11741179
args["data_frame"] = df_all_trees
@@ -1222,7 +1227,7 @@ def infer_config(args, constructor, trace_patch):
12221227
else:
12231228
if (
12241229
args["color"]
1225-
and args["data_frame"][args["color"]].dtype.kind in "bifc"
1230+
and args["data_frame"][args["color"]].dtype.kind in "ifc"
12261231
):
12271232
attrs.append("color")
12281233
args["color_is_continuous"] = True

Diff for: test/percy/plotly-express.py

+36
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,39 @@
550550
y=["first", "second", "first", "second"], x=[3, 1, 4, 2], color=["A", "A", "B", "B"]
551551
)
552552
fig.write_html(os.path.join(dir_name, "funnel.html"), auto_play=False)
553+
554+
import plotly.express as px
555+
556+
fig = px.scatter(x=[1, 2, 1, 2], y=[4, 3, 2, 4], color=[True, True, False, False])
557+
fig.write_html(os.path.join(dir_name, "scatter_bool_color.html"), auto_play=False)
558+
559+
560+
import plotly.express as px
561+
562+
fig = px.pie(values=[1, 2, 3, 4], color=[True, False, True, False])
563+
fig.write_html(os.path.join(dir_name, "pie_bool_color.html"), auto_play=False)
564+
565+
import plotly.express as px
566+
import numpy as np
567+
568+
df = px.data.gapminder().query("year == 2007")
569+
np.random.seed(0)
570+
df["color"] = np.random.choice([True, False], len(df))
571+
fig = px.choropleth(df, locations="iso_alpha", color="color")
572+
fig.write_html(os.path.join(dir_name, "choropleth_bool_color.html"), auto_play=False)
573+
574+
import plotly.express as px
575+
576+
df = px.data.iris()
577+
df["is_setosa"] = df["species"] == "setosa"
578+
fig = px.density_contour(df, x="sepal_width", y="sepal_length", color="is_setosa")
579+
fig.write_html(
580+
os.path.join(dir_name, "density_contour_bool_color.html"), auto_play=False
581+
)
582+
583+
import plotly.express as px
584+
585+
fig = px.sunburst(
586+
path=[["yes", "no", "no"], ["yes", "no", "a"]], color=[True, False, True]
587+
)
588+
fig.write_html(os.path.join(dir_name, "sunburst_bool_color.html"), auto_play=False)

0 commit comments

Comments
 (0)