Skip to content

Commit

Permalink
Px bool (#2127)
Browse files Browse the repository at this point in the history
* removed booleans from continuous color types

* bifc -> ifc

* fix bug

* added tests and better handling of corner case for naming additional column

* blacken
  • Loading branch information
emmanuelle authored Mar 11, 2020
1 parent 15ab76f commit 307fd73
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions test/percy/plotly-express.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 307fd73

Please sign in to comment.