Skip to content

Commit

Permalink
initial pass at PX auto-orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaskruchten committed Mar 31, 2020
1 parent d0b8f9f commit 1cff5c0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 35 deletions.
40 changes: 11 additions & 29 deletions packages/python/plotly/plotly/express/_chart_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def area(
labels={},
color_discrete_sequence=None,
color_discrete_map={},
orientation="v",
orientation=None,
groupnorm=None,
log_x=False,
log_y=False,
Expand All @@ -256,9 +256,7 @@ def area(
return make_figure(
args=locals(),
constructor=go.Scatter,
trace_patch=dict(
stackgroup=1, mode="lines", orientation=orientation, groupnorm=groupnorm
),
trace_patch=dict(stackgroup=1, mode="lines", groupnorm=groupnorm),
)


Expand Down Expand Up @@ -291,7 +289,7 @@ def bar(
range_color=None,
color_continuous_midpoint=None,
opacity=None,
orientation="v",
orientation=None,
barmode="relative",
log_x=False,
log_y=False,
Expand Down Expand Up @@ -335,7 +333,7 @@ def histogram(
color_discrete_map={},
marginal=None,
opacity=None,
orientation="v",
orientation=None,
barmode="relative",
barnorm=None,
histnorm=None,
Expand All @@ -361,13 +359,7 @@ def histogram(
args=locals(),
constructor=go.Histogram,
trace_patch=dict(
orientation=orientation,
histnorm=histnorm,
histfunc=histfunc,
nbinsx=nbins if orientation == "v" else None,
nbinsy=None if orientation == "v" else nbins,
cumulative=dict(enabled=cumulative),
bingroup="x" if orientation == "v" else "y",
histnorm=histnorm, histfunc=histfunc, cumulative=dict(enabled=cumulative),
),
layout_patch=dict(barmode=barmode, barnorm=barnorm),
)
Expand All @@ -393,7 +385,7 @@ def violin(
labels={},
color_discrete_sequence=None,
color_discrete_map={},
orientation="v",
orientation=None,
violinmode="group",
log_x=False,
log_y=False,
Expand All @@ -414,12 +406,7 @@ def violin(
args=locals(),
constructor=go.Violin,
trace_patch=dict(
orientation=orientation,
points=points,
box=dict(visible=box),
scalegroup=True,
x0=" ",
y0=" ",
points=points, box=dict(visible=box), scalegroup=True, x0=" ", y0=" ",
),
layout_patch=dict(violinmode=violinmode),
)
Expand All @@ -445,7 +432,7 @@ def box(
labels={},
color_discrete_sequence=None,
color_discrete_map={},
orientation="v",
orientation=None,
boxmode="group",
log_x=False,
log_y=False,
Expand All @@ -470,9 +457,7 @@ def box(
return make_figure(
args=locals(),
constructor=go.Box,
trace_patch=dict(
orientation=orientation, boxpoints=points, notched=notched, x0=" ", y0=" "
),
trace_patch=dict(boxpoints=points, notched=notched, x0=" ", y0=" "),
layout_patch=dict(boxmode=boxmode),
)

Expand All @@ -497,7 +482,7 @@ def strip(
labels={},
color_discrete_sequence=None,
color_discrete_map={},
orientation="v",
orientation=None,
stripmode="group",
log_x=False,
log_y=False,
Expand All @@ -516,7 +501,6 @@ def strip(
args=locals(),
constructor=go.Box,
trace_patch=dict(
orientation=orientation,
boxpoints="all",
pointpos=0,
hoveron="points",
Expand Down Expand Up @@ -1398,9 +1382,7 @@ def funnel(
rectangular sector of a funnel.
"""
return make_figure(
args=locals(),
constructor=go.Funnel,
trace_patch=dict(opacity=opacity, orientation=orientation),
args=locals(), constructor=go.Funnel, trace_patch=dict(opacity=opacity),
)


Expand Down
43 changes: 37 additions & 6 deletions packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ def get_label(args, column):
return column


def _is_continuous(df, col_name):
return df[col_name].dtype.kind in "ifc"


def get_decorated_label(args, column, role):
label = get_label(args, column)
if "histfunc" in args and (
Expand Down Expand Up @@ -188,7 +192,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 "ifc"
or _is_continuous(args["data_frame"], name)
)
and (
trace_spec.constructor != go.Parcats
Expand Down Expand Up @@ -1124,7 +1128,7 @@ def aggfunc_discrete(x):
agg_f[count_colname] = "sum"

if args["color"]:
if df[args["color"]].dtype.kind not in "ifc":
if not _is_continuous(df, args["color"]):
aggfunc_color = aggfunc_discrete
discrete_color = True
elif not aggfunc_color:
Expand Down Expand Up @@ -1212,6 +1216,36 @@ def infer_config(args, constructor, trace_patch):
if constructor in [go.Treemap, go.Sunburst] and args["path"] is not None:
args = process_dataframe_hierarchy(args)

if "orientation" in args:
has_x = args["x"] is not None
has_y = args["y"] is not None
if args["orientation"] is None:
if constructor in [go.Histogram, go.Scatter]:
if has_y and not has_x:
args["orientation"] = "h"
elif constructor in [go.Violin, go.Box, go.Bar, go.Funnel]:
if has_x and not has_y:
args["orientation"] = "h"

if args["orientation"] is None and has_x and has_y:
x_is_continuous = _is_continuous(args["data_frame"], args["x"])
y_is_continuous = _is_continuous(args["data_frame"], args["y"])
if x_is_continuous and not y_is_continuous:
args["orientation"] = "h"
if y_is_continuous and not x_is_continuous:
args["orientation"] = "v"

if args["orientation"] is None:
args["orientation"] = "v"

if constructor == go.Histogram:
orientation = args["orientation"]
nbins = args["nbins"]
trace_patch["nbinsx"] = nbins if orientation == "v" else None
trace_patch["nbinsy"] = None if orientation == "v" else nbins
trace_patch["bingroup"] = "x" if orientation == "v" else "y"
trace_patch["orientation"] = args["orientation"]

attrs = [k for k in attrables if k in args]
grouped_attrs = []

Expand All @@ -1226,10 +1260,7 @@ def infer_config(args, constructor, trace_patch):
if "color_discrete_sequence" not in args:
attrs.append("color")
else:
if (
args["color"]
and args["data_frame"][args["color"]].dtype.kind in "ifc"
):
if args["color"] and _is_continuous(args["data_frame"], args["color"]):
attrs.append("color")
args["color_is_continuous"] = True
elif constructor in [go.Sunburst, go.Treemap]:
Expand Down

0 comments on commit 1cff5c0

Please sign in to comment.