Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 110 additions & 97 deletions packages/python/plotly/plotly/express/_core.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -137,7 +137,7 @@ def make_mapping(args, variable):
)


def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
"""Populates a dict with arguments to update trace

Parameters
Expand All@@ -147,7 +147,7 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
trace_spec : NamedTuple
which kind of trace to be used (has constructor, marginal etc.
attributes)
g : pandas DataFrame
trace_data : pandas DataFrame
data
mapping_labels : dict
to be used for hovertemplate
Expand All@@ -156,87 +156,92 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):

Returns
-------
result : dict
trace_patch : dict
dict to be used to update trace
fit_results : dict
fit information to be used for trendlines
"""
if "line_close" in args and args["line_close"]:
g = g.append(g.iloc[0])
result = trace_spec.trace_patch.copy() or {}
trace_data = trace_data.append(trace_data.iloc[0])
trace_patch = trace_spec.trace_patch.copy() or {}
fit_results = None
hover_header = ""
custom_data_len = 0
for k in trace_spec.attrs:
v = args[k]
v_label = get_decorated_label(args, v, k)
if k == "dimensions":
for attr_name in trace_spec.attrs:
attr_value = args[attr_name]
attr_label = get_decorated_label(args, attr_value, attr_name)
if attr_name == "dimensions":
dims = [
(name, column)
for (name, column) in g.iteritems()
if ((not v) or (name in v))
for (name, column) in trace_data.iteritems()
if ((not attr_value) or (name in attr_value))
and (
trace_spec.constructor != go.Parcoords
or args["data_frame"][name].dtype.kind in "bifc"
)
and (
trace_spec.constructor != go.Parcats
or (v is not None and name in v)
or (attr_value is not None and name in attr_value)
or len(args["data_frame"][name].unique())
<= args["dimensions_max_cardinality"]
)
]
result["dimensions"] = [
trace_patch["dimensions"] = [
dict(label=get_label(args, name), values=column.values)
for (name, column) in dims
]
if trace_spec.constructor == go.Splom:
for d in result["dimensions"]:
for d in trace_patch["dimensions"]:
d["axis"] = dict(matches=True)
mapping_labels["%{xaxis.title.text}"] = "%{x}"
mapping_labels["%{yaxis.title.text}"] = "%{y}"

elif (
v is not None
or (trace_spec.constructor == go.Histogram and k in ["x", "y"])
attr_value is not None
or (trace_spec.constructor == go.Histogram and attr_name in ["x", "y"])
or (
trace_spec.constructor in [go.Histogram2d, go.Histogram2dContour]
and k == "z"
and attr_name == "z"
)
):
if k == "size":
if "marker" not in result:
result["marker"] = dict()
result["marker"]["size"] = g[v]
result["marker"]["sizemode"] = "area"
result["marker"]["sizeref"] = sizeref
mapping_labels[v_label] = "%{marker.size}"
elif k == "marginal_x":
if attr_name == "size":
if "marker" not in trace_patch:
trace_patch["marker"] = dict()
trace_patch["marker"]["size"] = trace_data[attr_value]
trace_patch["marker"]["sizemode"] = "area"
trace_patch["marker"]["sizeref"] = sizeref
mapping_labels[attr_label] = "%{marker.size}"
elif attr_name == "marginal_x":
if trace_spec.constructor == go.Histogram:
mapping_labels["count"] = "%{y}"
elif k == "marginal_y":
elif attr_name == "marginal_y":
if trace_spec.constructor == go.Histogram:
mapping_labels["count"] = "%{x}"
elif k == "trendline":
if v in ["ols", "lowess"] and args["x"] and args["y"] and len(g) > 1:
elif attr_name == "trendline":
if (
attr_value in ["ols", "lowess"]
and args["x"]
and args["y"]
and len(trace_data) > 1
):
import statsmodels.api as sm

# sorting is bad but trace_specs with "trendline" have no other attrs
g2 = g.sort_values(by=args["x"])
y = g2[args["y"]]
x = g2[args["x"]]
result["x"] = x
sorted_trace_data = trace_data.sort_values(by=args["x"])
y = sorted_trace_data[args["y"]]
x = sorted_trace_data[args["x"]]
trace_patch["x"] = x

if x.dtype.type == np.datetime64:
x = x.astype(int) / 10 ** 9 # convert to unix epoch seconds

if v == "lowess":
if attr_value == "lowess":
trendline = sm.nonparametric.lowess(y, x)
result["y"] = trendline[:, 1]
trace_patch["y"] = trendline[:, 1]
hover_header = "<b>LOWESS trendline</b><br><br>"
elif v == "ols":
elif attr_value == "ols":
fit_results = sm.OLS(y.values, sm.add_constant(x.values)).fit()
result["y"] = fit_results.predict()
trace_patch["y"] = fit_results.predict()
hover_header = "<b>OLS trendline</b><br>"
hover_header += "%s = %g * %s + %g<br>" % (
args["y"],
Expand All@@ -250,120 +255,127 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
mapping_labels[get_label(args, args["x"])] = "%{x}"
mapping_labels[get_label(args, args["y"])] = "%{y} <b>(trend)</b>"

elif k.startswith("error"):
error_xy = k[:7]
arr = "arrayminus" if k.endswith("minus") else "array"
if error_xy not in result:
result[error_xy] = {}
result[error_xy][arr] = g[v]
elif k == "custom_data":
result["customdata"] = g[v].values
custom_data_len = len(v) # number of custom data columns
elif k == "hover_name":
elif attr_name.startswith("error"):
error_xy = attr_name[:7]
arr = "arrayminus" if attr_name.endswith("minus") else "array"
if error_xy not in trace_patch:
trace_patch[error_xy] = {}
trace_patch[error_xy][arr] = trace_data[attr_value]
elif attr_name == "custom_data":
trace_patch["customdata"] = trace_data[attr_value].values
custom_data_len = len(attr_value) # number of custom data columns
elif attr_name == "hover_name":
if trace_spec.constructor not in [
go.Histogram,
go.Histogram2d,
go.Histogram2dContour,
]:
result["hovertext"] = g[v]
trace_patch["hovertext"] = trace_data[attr_value]
if hover_header == "":
hover_header = "<b>%{hovertext}</b><br><br>"
elif k == "hover_data":
elif attr_name == "hover_data":
if trace_spec.constructor not in [
go.Histogram,
go.Histogram2d,
go.Histogram2dContour,
]:
for col in v:
for col in attr_value:
try:
position = args["custom_data"].index(col)
except (ValueError, AttributeError, KeyError):
position = custom_data_len
custom_data_len += 1
if "customdata" in result:
result["customdata"] = np.hstack(
(result["customdata"], g[col].values[:, None])
if "customdata" in trace_patch:
trace_patch["customdata"] = np.hstack(
(
trace_patch["customdata"],
trace_data[col].values[:, None],
)
)
else:
result["customdata"] = g[col].values[:, None]
v_label_col = get_decorated_label(args, col, None)
mapping_labels[v_label_col] = "%%{customdata[%d]}" % (position)
elif k == "color":
trace_patch["customdata"] = trace_data[col].values[
:, None
]
attr_label_col = get_decorated_label(args, col, None)
mapping_labels[attr_label_col] = "%%{customdata[%d]}" % (
position
)
elif attr_name == "color":
if trace_spec.constructor in [go.Choropleth, go.Choroplethmapbox]:
result["z"] = g[v]
result["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{z}"
trace_patch["z"] = trace_data[attr_value]
trace_patch["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%{z}"
elif trace_spec.constructor in [
go.Sunburst,
go.Treemap,
go.Pie,
go.Funnelarea,
]:
if "marker" not in result:
result["marker"] = dict()
if "marker" not in trace_patch:
trace_patch["marker"] = dict()

if args.get("color_is_continuous"):
result["marker"]["colors"] = g[v]
result["marker"]["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{color}"
trace_patch["marker"]["colors"] = trace_data[attr_value]
trace_patch["marker"]["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%{color}"
else:
result["marker"]["colors"] = []
trace_patch["marker"]["colors"] = []
mapping = {}
for cat in g[v]:
for cat in trace_data[attr_value]:
if mapping.get(cat) is None:
mapping[cat] = args["color_discrete_sequence"][
len(mapping) % len(args["color_discrete_sequence"])
]
result["marker"]["colors"].append(mapping[cat])
trace_patch["marker"]["colors"].append(mapping[cat])
else:
colorable = "marker"
if trace_spec.constructor in [go.Parcats, go.Parcoords]:
colorable = "line"
if colorable not in result:
result[colorable] = dict()
result[colorable]["color"] = g[v]
result[colorable]["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%%{%s.color}" % colorable
elif k == "animation_group":
result["ids"] = g[v]
elif k == "locations":
result[k] = g[v]
mapping_labels[v_label] = "%{location}"
elif k == "values":
result[k] = g[v]
_label = "value" if v_label == "values" else v_label
if colorable not in trace_patch:
trace_patch[colorable] = dict()
trace_patch[colorable]["color"] = trace_data[attr_value]
trace_patch[colorable]["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%%{%s.color}" % colorable
elif attr_name == "animation_group":
trace_patch["ids"] = trace_data[attr_value]
elif attr_name == "locations":
trace_patch[attr_name] = trace_data[attr_value]
mapping_labels[attr_label] = "%{location}"
elif attr_name == "values":
trace_patch[attr_name] = trace_data[attr_value]
_label = "value" if attr_label == "values" else attr_label
mapping_labels[_label] = "%{value}"
elif k == "parents":
result[k] = g[v]
_label = "parent" if v_label == "parents" else v_label
elif attr_name == "parents":
trace_patch[attr_name] = trace_data[attr_value]
_label = "parent" if attr_label == "parents" else attr_label
mapping_labels[_label] = "%{parent}"
elif k == "ids":
result[k] = g[v]
_label = "id" if v_label == "ids" else v_label
elif attr_name == "ids":
trace_patch[attr_name] = trace_data[attr_value]
_label = "id" if attr_label == "ids" else attr_label
mapping_labels[_label] = "%{id}"
elif k == "names":
elif attr_name == "names":
if trace_spec.constructor in [
go.Sunburst,
go.Treemap,
go.Pie,
go.Funnelarea,
]:
result["labels"] = g[v]
_label = "label" if v_label == "names" else v_label
trace_patch["labels"] = trace_data[attr_value]
_label = "label" if attr_label == "names" else attr_label
mapping_labels[_label] = "%{label}"
else:
result[k] = g[v]
trace_patch[attr_name] = trace_data[attr_value]
else:
if v:
result[k] = g[v]
mapping_labels[v_label] = "%%{%s}" % k
if attr_value:
trace_patch[attr_name] = trace_data[attr_value]
mapping_labels[attr_label] = "%%{%s}" % attr_name
if trace_spec.constructor not in [
go.Parcoords,
go.Parcats,
]:
hover_lines = [k + "=" + v for k, v in mapping_labels.items()]
result["hovertemplate"] = hover_header + "<br>".join(hover_lines)
return result, fit_results
trace_patch["hovertemplate"] = hover_header + "<br>".join(hover_lines)
return trace_patch, fit_results


def configure_axes(args, constructor, fig, orders):
Expand DownExpand Up@@ -1015,8 +1027,8 @@ def _check_dataframe_all_leaves(df):
null_indices = np.nonzero(null_mask.any(axis=1).values)[0]
for null_row_index in null_indices:
row = null_mask.iloc[null_row_index]
indices = np.nonzero(row.values)[0]
if not row[indices[0] :].all():
i = np.nonzero(row.values)[0][0]
if not row[i:].all():
raise ValueError(
"None entries cannot have not-None children",
df_sorted.iloc[null_row_index],
Expand DownExpand Up@@ -1058,6 +1070,7 @@ def process_dataframe_hierarchy(args):
path = [new_col_name if x == col_name else x for x in path]
df[new_col_name] = series_to_copy
# ------------ Define aggregation functions --------------------------------

def aggfunc_discrete(x):
uniques = x.unique()
if len(uniques) == 1:
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 110 additions & 97 deletions packages/python/plotly/plotly/express/_core.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -137,7 +137,7 @@ def make_mapping(args, variable):
)


def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
"""Populates a dict with arguments to update trace

Parameters
Expand All@@ -147,7 +147,7 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
trace_spec : NamedTuple
which kind of trace to be used (has constructor, marginal etc.
attributes)
g : pandas DataFrame
trace_data : pandas DataFrame
data
mapping_labels : dict
to be used for hovertemplate
Expand All@@ -156,87 +156,92 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):

Returns
-------
result : dict
trace_patch : dict
dict to be used to update trace
fit_results : dict
fit information to be used for trendlines
"""
if "line_close" in args and args["line_close"]:
g = g.append(g.iloc[0])
result = trace_spec.trace_patch.copy() or {}
trace_data = trace_data.append(trace_data.iloc[0])
trace_patch = trace_spec.trace_patch.copy() or {}
fit_results = None
hover_header = ""
custom_data_len = 0
for k in trace_spec.attrs:
v = args[k]
v_label = get_decorated_label(args, v, k)
if k == "dimensions":
for attr_name in trace_spec.attrs:
attr_value = args[attr_name]
attr_label = get_decorated_label(args, attr_value, attr_name)
if attr_name == "dimensions":
dims = [
(name, column)
for (name, column) in g.iteritems()
if ((not v) or (name in v))
for (name, column) in trace_data.iteritems()
if ((not attr_value) or (name in attr_value))
and (
trace_spec.constructor != go.Parcoords
or args["data_frame"][name].dtype.kind in "bifc"
)
and (
trace_spec.constructor != go.Parcats
or (v is not None and name in v)
or (attr_value is not None and name in attr_value)
or len(args["data_frame"][name].unique())
<= args["dimensions_max_cardinality"]
)
]
result["dimensions"] = [
trace_patch["dimensions"] = [
dict(label=get_label(args, name), values=column.values)
for (name, column) in dims
]
if trace_spec.constructor == go.Splom:
for d in result["dimensions"]:
for d in trace_patch["dimensions"]:
d["axis"] = dict(matches=True)
mapping_labels["%{xaxis.title.text}"] = "%{x}"
mapping_labels["%{yaxis.title.text}"] = "%{y}"

elif (
v is not None
or (trace_spec.constructor == go.Histogram and k in ["x", "y"])
attr_value is not None
or (trace_spec.constructor == go.Histogram and attr_name in ["x", "y"])
or (
trace_spec.constructor in [go.Histogram2d, go.Histogram2dContour]
and k == "z"
and attr_name == "z"
)
):
if k == "size":
if "marker" not in result:
result["marker"] = dict()
result["marker"]["size"] = g[v]
result["marker"]["sizemode"] = "area"
result["marker"]["sizeref"] = sizeref
mapping_labels[v_label] = "%{marker.size}"
elif k == "marginal_x":
if attr_name == "size":
if "marker" not in trace_patch:
trace_patch["marker"] = dict()
trace_patch["marker"]["size"] = trace_data[attr_value]
trace_patch["marker"]["sizemode"] = "area"
trace_patch["marker"]["sizeref"] = sizeref
mapping_labels[attr_label] = "%{marker.size}"
elif attr_name == "marginal_x":
if trace_spec.constructor == go.Histogram:
mapping_labels["count"] = "%{y}"
elif k == "marginal_y":
elif attr_name == "marginal_y":
if trace_spec.constructor == go.Histogram:
mapping_labels["count"] = "%{x}"
elif k == "trendline":
if v in ["ols", "lowess"] and args["x"] and args["y"] and len(g) > 1:
elif attr_name == "trendline":
if (
attr_value in ["ols", "lowess"]
and args["x"]
and args["y"]
and len(trace_data) > 1
):
import statsmodels.api as sm

# sorting is bad but trace_specs with "trendline" have no other attrs
g2 = g.sort_values(by=args["x"])
y = g2[args["y"]]
x = g2[args["x"]]
result["x"] = x
sorted_trace_data = trace_data.sort_values(by=args["x"])
y = sorted_trace_data[args["y"]]
x = sorted_trace_data[args["x"]]
trace_patch["x"] = x

if x.dtype.type == np.datetime64:
x = x.astype(int) / 10 ** 9 # convert to unix epoch seconds

if v == "lowess":
if attr_value == "lowess":
trendline = sm.nonparametric.lowess(y, x)
result["y"] = trendline[:, 1]
trace_patch["y"] = trendline[:, 1]
hover_header = "<b>LOWESS trendline</b><br><br>"
elif v == "ols":
elif attr_value == "ols":
fit_results = sm.OLS(y.values, sm.add_constant(x.values)).fit()
result["y"] = fit_results.predict()
trace_patch["y"] = fit_results.predict()
hover_header = "<b>OLS trendline</b><br>"
hover_header += "%s = %g * %s + %g<br>" % (
args["y"],
Expand All@@ -250,120 +255,127 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
mapping_labels[get_label(args, args["x"])] = "%{x}"
mapping_labels[get_label(args, args["y"])] = "%{y} <b>(trend)</b>"

elif k.startswith("error"):
error_xy = k[:7]
arr = "arrayminus" if k.endswith("minus") else "array"
if error_xy not in result:
result[error_xy] = {}
result[error_xy][arr] = g[v]
elif k == "custom_data":
result["customdata"] = g[v].values
custom_data_len = len(v) # number of custom data columns
elif k == "hover_name":
elif attr_name.startswith("error"):
error_xy = attr_name[:7]
arr = "arrayminus" if attr_name.endswith("minus") else "array"
if error_xy not in trace_patch:
trace_patch[error_xy] = {}
trace_patch[error_xy][arr] = trace_data[attr_value]
elif attr_name == "custom_data":
trace_patch["customdata"] = trace_data[attr_value].values
custom_data_len = len(attr_value) # number of custom data columns
elif attr_name == "hover_name":
if trace_spec.constructor not in [
go.Histogram,
go.Histogram2d,
go.Histogram2dContour,
]:
result["hovertext"] = g[v]
trace_patch["hovertext"] = trace_data[attr_value]
if hover_header == "":
hover_header = "<b>%{hovertext}</b><br><br>"
elif k == "hover_data":
elif attr_name == "hover_data":
if trace_spec.constructor not in [
go.Histogram,
go.Histogram2d,
go.Histogram2dContour,
]:
for col in v:
for col in attr_value:
try:
position = args["custom_data"].index(col)
except (ValueError, AttributeError, KeyError):
position = custom_data_len
custom_data_len += 1
if "customdata" in result:
result["customdata"] = np.hstack(
(result["customdata"], g[col].values[:, None])
if "customdata" in trace_patch:
trace_patch["customdata"] = np.hstack(
(
trace_patch["customdata"],
trace_data[col].values[:, None],
)
)
else:
result["customdata"] = g[col].values[:, None]
v_label_col = get_decorated_label(args, col, None)
mapping_labels[v_label_col] = "%%{customdata[%d]}" % (position)
elif k == "color":
trace_patch["customdata"] = trace_data[col].values[
:, None
]
attr_label_col = get_decorated_label(args, col, None)
mapping_labels[attr_label_col] = "%%{customdata[%d]}" % (
position
)
elif attr_name == "color":
if trace_spec.constructor in [go.Choropleth, go.Choroplethmapbox]:
result["z"] = g[v]
result["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{z}"
trace_patch["z"] = trace_data[attr_value]
trace_patch["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%{z}"
elif trace_spec.constructor in [
go.Sunburst,
go.Treemap,
go.Pie,
go.Funnelarea,
]:
if "marker" not in result:
result["marker"] = dict()
if "marker" not in trace_patch:
trace_patch["marker"] = dict()

if args.get("color_is_continuous"):
result["marker"]["colors"] = g[v]
result["marker"]["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{color}"
trace_patch["marker"]["colors"] = trace_data[attr_value]
trace_patch["marker"]["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%{color}"
else:
result["marker"]["colors"] = []
trace_patch["marker"]["colors"] = []
mapping = {}
for cat in g[v]:
for cat in trace_data[attr_value]:
if mapping.get(cat) is None:
mapping[cat] = args["color_discrete_sequence"][
len(mapping) % len(args["color_discrete_sequence"])
]
result["marker"]["colors"].append(mapping[cat])
trace_patch["marker"]["colors"].append(mapping[cat])
else:
colorable = "marker"
if trace_spec.constructor in [go.Parcats, go.Parcoords]:
colorable = "line"
if colorable not in result:
result[colorable] = dict()
result[colorable]["color"] = g[v]
result[colorable]["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%%{%s.color}" % colorable
elif k == "animation_group":
result["ids"] = g[v]
elif k == "locations":
result[k] = g[v]
mapping_labels[v_label] = "%{location}"
elif k == "values":
result[k] = g[v]
_label = "value" if v_label == "values" else v_label
if colorable not in trace_patch:
trace_patch[colorable] = dict()
trace_patch[colorable]["color"] = trace_data[attr_value]
trace_patch[colorable]["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%%{%s.color}" % colorable
elif attr_name == "animation_group":
trace_patch["ids"] = trace_data[attr_value]
elif attr_name == "locations":
trace_patch[attr_name] = trace_data[attr_value]
mapping_labels[attr_label] = "%{location}"
elif attr_name == "values":
trace_patch[attr_name] = trace_data[attr_value]
_label = "value" if attr_label == "values" else attr_label
mapping_labels[_label] = "%{value}"
elif k == "parents":
result[k] = g[v]
_label = "parent" if v_label == "parents" else v_label
elif attr_name == "parents":
trace_patch[attr_name] = trace_data[attr_value]
_label = "parent" if attr_label == "parents" else attr_label
mapping_labels[_label] = "%{parent}"
elif k == "ids":
result[k] = g[v]
_label = "id" if v_label == "ids" else v_label
elif attr_name == "ids":
trace_patch[attr_name] = trace_data[attr_value]
_label = "id" if attr_label == "ids" else attr_label
mapping_labels[_label] = "%{id}"
elif k == "names":
elif attr_name == "names":
if trace_spec.constructor in [
go.Sunburst,
go.Treemap,
go.Pie,
go.Funnelarea,
]:
result["labels"] = g[v]
_label = "label" if v_label == "names" else v_label
trace_patch["labels"] = trace_data[attr_value]
_label = "label" if attr_label == "names" else attr_label
mapping_labels[_label] = "%{label}"
else:
result[k] = g[v]
trace_patch[attr_name] = trace_data[attr_value]
else:
if v:
result[k] = g[v]
mapping_labels[v_label] = "%%{%s}" % k
if attr_value:
trace_patch[attr_name] = trace_data[attr_value]
mapping_labels[attr_label] = "%%{%s}" % attr_name
if trace_spec.constructor not in [
go.Parcoords,
go.Parcats,
]:
hover_lines = [k + "=" + v for k, v in mapping_labels.items()]
result["hovertemplate"] = hover_header + "<br>".join(hover_lines)
return result, fit_results
trace_patch["hovertemplate"] = hover_header + "<br>".join(hover_lines)
return trace_patch, fit_results


def configure_axes(args, constructor, fig, orders):
Expand DownExpand Up@@ -1015,8 +1027,8 @@ def _check_dataframe_all_leaves(df):
null_indices = np.nonzero(null_mask.any(axis=1).values)[0]
for null_row_index in null_indices:
row = null_mask.iloc[null_row_index]
indices = np.nonzero(row.values)[0]
if not row[indices[0] :].all():
i = np.nonzero(row.values)[0][0]
if not row[i:].all():
raise ValueError(
"None entries cannot have not-None children",
df_sorted.iloc[null_row_index],
Expand DownExpand Up@@ -1058,6 +1070,7 @@ def process_dataframe_hierarchy(args):
path = [new_col_name if x == col_name else x for x in path]
df[new_col_name] = series_to_copy
# ------------ Define aggregation functions --------------------------------

def aggfunc_discrete(x):
uniques = x.unique()
if len(uniques) == 1:
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 110 additions & 97 deletions packages/python/plotly/plotly/express/_core.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -137,7 +137,7 @@ def make_mapping(args, variable):
)


def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
"""Populates a dict with arguments to update trace

Parameters
Expand All@@ -147,7 +147,7 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
trace_spec : NamedTuple
which kind of trace to be used (has constructor, marginal etc.
attributes)
g : pandas DataFrame
trace_data : pandas DataFrame
data
mapping_labels : dict
to be used for hovertemplate
Expand All@@ -156,87 +156,92 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):

Returns
-------
result : dict
trace_patch : dict
dict to be used to update trace
fit_results : dict
fit information to be used for trendlines
"""
if "line_close" in args and args["line_close"]:
g = g.append(g.iloc[0])
result = trace_spec.trace_patch.copy() or {}
trace_data = trace_data.append(trace_data.iloc[0])
trace_patch = trace_spec.trace_patch.copy() or {}
fit_results = None
hover_header = ""
custom_data_len = 0
for k in trace_spec.attrs:
v = args[k]
v_label = get_decorated_label(args, v, k)
if k == "dimensions":
for attr_name in trace_spec.attrs:
attr_value = args[attr_name]
attr_label = get_decorated_label(args, attr_value, attr_name)
if attr_name == "dimensions":
dims = [
(name, column)
for (name, column) in g.iteritems()
if ((not v) or (name in v))
for (name, column) in trace_data.iteritems()
if ((not attr_value) or (name in attr_value))
and (
trace_spec.constructor != go.Parcoords
or args["data_frame"][name].dtype.kind in "bifc"
)
and (
trace_spec.constructor != go.Parcats
or (v is not None and name in v)
or (attr_value is not None and name in attr_value)
or len(args["data_frame"][name].unique())
<= args["dimensions_max_cardinality"]
)
]
result["dimensions"] = [
trace_patch["dimensions"] = [
dict(label=get_label(args, name), values=column.values)
for (name, column) in dims
]
if trace_spec.constructor == go.Splom:
for d in result["dimensions"]:
for d in trace_patch["dimensions"]:
d["axis"] = dict(matches=True)
mapping_labels["%{xaxis.title.text}"] = "%{x}"
mapping_labels["%{yaxis.title.text}"] = "%{y}"

elif (
v is not None
or (trace_spec.constructor == go.Histogram and k in ["x", "y"])
attr_value is not None
or (trace_spec.constructor == go.Histogram and attr_name in ["x", "y"])
or (
trace_spec.constructor in [go.Histogram2d, go.Histogram2dContour]
and k == "z"
and attr_name == "z"
)
):
if k == "size":
if "marker" not in result:
result["marker"] = dict()
result["marker"]["size"] = g[v]
result["marker"]["sizemode"] = "area"
result["marker"]["sizeref"] = sizeref
mapping_labels[v_label] = "%{marker.size}"
elif k == "marginal_x":
if attr_name == "size":
if "marker" not in trace_patch:
trace_patch["marker"] = dict()
trace_patch["marker"]["size"] = trace_data[attr_value]
trace_patch["marker"]["sizemode"] = "area"
trace_patch["marker"]["sizeref"] = sizeref
mapping_labels[attr_label] = "%{marker.size}"
elif attr_name == "marginal_x":
if trace_spec.constructor == go.Histogram:
mapping_labels["count"] = "%{y}"
elif k == "marginal_y":
elif attr_name == "marginal_y":
if trace_spec.constructor == go.Histogram:
mapping_labels["count"] = "%{x}"
elif k == "trendline":
if v in ["ols", "lowess"] and args["x"] and args["y"] and len(g) > 1:
elif attr_name == "trendline":
if (
attr_value in ["ols", "lowess"]
and args["x"]
and args["y"]
and len(trace_data) > 1
):
import statsmodels.api as sm

# sorting is bad but trace_specs with "trendline" have no other attrs
g2 = g.sort_values(by=args["x"])
y = g2[args["y"]]
x = g2[args["x"]]
result["x"] = x
sorted_trace_data = trace_data.sort_values(by=args["x"])
y = sorted_trace_data[args["y"]]
x = sorted_trace_data[args["x"]]
trace_patch["x"] = x

if x.dtype.type == np.datetime64:
x = x.astype(int) / 10 ** 9 # convert to unix epoch seconds

if v == "lowess":
if attr_value == "lowess":
trendline = sm.nonparametric.lowess(y, x)
result["y"] = trendline[:, 1]
trace_patch["y"] = trendline[:, 1]
hover_header = "<b>LOWESS trendline</b><br><br>"
elif v == "ols":
elif attr_value == "ols":
fit_results = sm.OLS(y.values, sm.add_constant(x.values)).fit()
result["y"] = fit_results.predict()
trace_patch["y"] = fit_results.predict()
hover_header = "<b>OLS trendline</b><br>"
hover_header += "%s = %g * %s + %g<br>" % (
args["y"],
Expand All@@ -250,120 +255,127 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
mapping_labels[get_label(args, args["x"])] = "%{x}"
mapping_labels[get_label(args, args["y"])] = "%{y} <b>(trend)</b>"

elif k.startswith("error"):
error_xy = k[:7]
arr = "arrayminus" if k.endswith("minus") else "array"
if error_xy not in result:
result[error_xy] = {}
result[error_xy][arr] = g[v]
elif k == "custom_data":
result["customdata"] = g[v].values
custom_data_len = len(v) # number of custom data columns
elif k == "hover_name":
elif attr_name.startswith("error"):
error_xy = attr_name[:7]
arr = "arrayminus" if attr_name.endswith("minus") else "array"
if error_xy not in trace_patch:
trace_patch[error_xy] = {}
trace_patch[error_xy][arr] = trace_data[attr_value]
elif attr_name == "custom_data":
trace_patch["customdata"] = trace_data[attr_value].values
custom_data_len = len(attr_value) # number of custom data columns
elif attr_name == "hover_name":
if trace_spec.constructor not in [
go.Histogram,
go.Histogram2d,
go.Histogram2dContour,
]:
result["hovertext"] = g[v]
trace_patch["hovertext"] = trace_data[attr_value]
if hover_header == "":
hover_header = "<b>%{hovertext}</b><br><br>"
elif k == "hover_data":
elif attr_name == "hover_data":
if trace_spec.constructor not in [
go.Histogram,
go.Histogram2d,
go.Histogram2dContour,
]:
for col in v:
for col in attr_value:
try:
position = args["custom_data"].index(col)
except (ValueError, AttributeError, KeyError):
position = custom_data_len
custom_data_len += 1
if "customdata" in result:
result["customdata"] = np.hstack(
(result["customdata"], g[col].values[:, None])
if "customdata" in trace_patch:
trace_patch["customdata"] = np.hstack(
(
trace_patch["customdata"],
trace_data[col].values[:, None],
)
)
else:
result["customdata"] = g[col].values[:, None]
v_label_col = get_decorated_label(args, col, None)
mapping_labels[v_label_col] = "%%{customdata[%d]}" % (position)
elif k == "color":
trace_patch["customdata"] = trace_data[col].values[
:, None
]
attr_label_col = get_decorated_label(args, col, None)
mapping_labels[attr_label_col] = "%%{customdata[%d]}" % (
position
)
elif attr_name == "color":
if trace_spec.constructor in [go.Choropleth, go.Choroplethmapbox]:
result["z"] = g[v]
result["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{z}"
trace_patch["z"] = trace_data[attr_value]
trace_patch["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%{z}"
elif trace_spec.constructor in [
go.Sunburst,
go.Treemap,
go.Pie,
go.Funnelarea,
]:
if "marker" not in result:
result["marker"] = dict()
if "marker" not in trace_patch:
trace_patch["marker"] = dict()

if args.get("color_is_continuous"):
result["marker"]["colors"] = g[v]
result["marker"]["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{color}"
trace_patch["marker"]["colors"] = trace_data[attr_value]
trace_patch["marker"]["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%{color}"
else:
result["marker"]["colors"] = []
trace_patch["marker"]["colors"] = []
mapping = {}
for cat in g[v]:
for cat in trace_data[attr_value]:
if mapping.get(cat) is None:
mapping[cat] = args["color_discrete_sequence"][
len(mapping) % len(args["color_discrete_sequence"])
]
result["marker"]["colors"].append(mapping[cat])
trace_patch["marker"]["colors"].append(mapping[cat])
else:
colorable = "marker"
if trace_spec.constructor in [go.Parcats, go.Parcoords]:
colorable = "line"
if colorable not in result:
result[colorable] = dict()
result[colorable]["color"] = g[v]
result[colorable]["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%%{%s.color}" % colorable
elif k == "animation_group":
result["ids"] = g[v]
elif k == "locations":
result[k] = g[v]
mapping_labels[v_label] = "%{location}"
elif k == "values":
result[k] = g[v]
_label = "value" if v_label == "values" else v_label
if colorable not in trace_patch:
trace_patch[colorable] = dict()
trace_patch[colorable]["color"] = trace_data[attr_value]
trace_patch[colorable]["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%%{%s.color}" % colorable
elif attr_name == "animation_group":
trace_patch["ids"] = trace_data[attr_value]
elif attr_name == "locations":
trace_patch[attr_name] = trace_data[attr_value]
mapping_labels[attr_label] = "%{location}"
elif attr_name == "values":
trace_patch[attr_name] = trace_data[attr_value]
_label = "value" if attr_label == "values" else attr_label
mapping_labels[_label] = "%{value}"
elif k == "parents":
result[k] = g[v]
_label = "parent" if v_label == "parents" else v_label
elif attr_name == "parents":
trace_patch[attr_name] = trace_data[attr_value]
_label = "parent" if attr_label == "parents" else attr_label
mapping_labels[_label] = "%{parent}"
elif k == "ids":
result[k] = g[v]
_label = "id" if v_label == "ids" else v_label
elif attr_name == "ids":
trace_patch[attr_name] = trace_data[attr_value]
_label = "id" if attr_label == "ids" else attr_label
mapping_labels[_label] = "%{id}"
elif k == "names":
elif attr_name == "names":
if trace_spec.constructor in [
go.Sunburst,
go.Treemap,
go.Pie,
go.Funnelarea,
]:
result["labels"] = g[v]
_label = "label" if v_label == "names" else v_label
trace_patch["labels"] = trace_data[attr_value]
_label = "label" if attr_label == "names" else attr_label
mapping_labels[_label] = "%{label}"
else:
result[k] = g[v]
trace_patch[attr_name] = trace_data[attr_value]
else:
if v:
result[k] = g[v]
mapping_labels[v_label] = "%%{%s}" % k
if attr_value:
trace_patch[attr_name] = trace_data[attr_value]
mapping_labels[attr_label] = "%%{%s}" % attr_name
if trace_spec.constructor not in [
go.Parcoords,
go.Parcats,
]:
hover_lines = [k + "=" + v for k, v in mapping_labels.items()]
result["hovertemplate"] = hover_header + "<br>".join(hover_lines)
return result, fit_results
trace_patch["hovertemplate"] = hover_header + "<br>".join(hover_lines)
return trace_patch, fit_results


def configure_axes(args, constructor, fig, orders):
Expand DownExpand Up@@ -1015,8 +1027,8 @@ def _check_dataframe_all_leaves(df):
null_indices = np.nonzero(null_mask.any(axis=1).values)[0]
for null_row_index in null_indices:
row = null_mask.iloc[null_row_index]
indices = np.nonzero(row.values)[0]
if not row[indices[0] :].all():
i = np.nonzero(row.values)[0][0]
if not row[i:].all():
raise ValueError(
"None entries cannot have not-None children",
df_sorted.iloc[null_row_index],
Expand DownExpand Up@@ -1058,6 +1070,7 @@ def process_dataframe_hierarchy(args):
path = [new_col_name if x == col_name else x for x in path]
df[new_col_name] = series_to_copy
# ------------ Define aggregation functions --------------------------------

def aggfunc_discrete(x):
uniques = x.unique()
if len(uniques) == 1:
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 110 additions & 97 deletions packages/python/plotly/plotly/express/_core.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -137,7 +137,7 @@ def make_mapping(args, variable):
)


def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
"""Populates a dict with arguments to update trace

Parameters
Expand All@@ -147,7 +147,7 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
trace_spec : NamedTuple
which kind of trace to be used (has constructor, marginal etc.
attributes)
g : pandas DataFrame
trace_data : pandas DataFrame
data
mapping_labels : dict
to be used for hovertemplate
Expand All@@ -156,87 +156,92 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):

Returns
-------
result : dict
trace_patch : dict
dict to be used to update trace
fit_results : dict
fit information to be used for trendlines
"""
if "line_close" in args and args["line_close"]:
g = g.append(g.iloc[0])
result = trace_spec.trace_patch.copy() or {}
trace_data = trace_data.append(trace_data.iloc[0])
trace_patch = trace_spec.trace_patch.copy() or {}
fit_results = None
hover_header = ""
custom_data_len = 0
for k in trace_spec.attrs:
v = args[k]
v_label = get_decorated_label(args, v, k)
if k == "dimensions":
for attr_name in trace_spec.attrs:
attr_value = args[attr_name]
attr_label = get_decorated_label(args, attr_value, attr_name)
if attr_name == "dimensions":
dims = [
(name, column)
for (name, column) in g.iteritems()
if ((not v) or (name in v))
for (name, column) in trace_data.iteritems()
if ((not attr_value) or (name in attr_value))
and (
trace_spec.constructor != go.Parcoords
or args["data_frame"][name].dtype.kind in "bifc"
)
and (
trace_spec.constructor != go.Parcats
or (v is not None and name in v)
or (attr_value is not None and name in attr_value)
or len(args["data_frame"][name].unique())
<= args["dimensions_max_cardinality"]
)
]
result["dimensions"] = [
trace_patch["dimensions"] = [
dict(label=get_label(args, name), values=column.values)
for (name, column) in dims
]
if trace_spec.constructor == go.Splom:
for d in result["dimensions"]:
for d in trace_patch["dimensions"]:
d["axis"] = dict(matches=True)
mapping_labels["%{xaxis.title.text}"] = "%{x}"
mapping_labels["%{yaxis.title.text}"] = "%{y}"

elif (
v is not None
or (trace_spec.constructor == go.Histogram and k in ["x", "y"])
attr_value is not None
or (trace_spec.constructor == go.Histogram and attr_name in ["x", "y"])
or (
trace_spec.constructor in [go.Histogram2d, go.Histogram2dContour]
and k == "z"
and attr_name == "z"
)
):
if k == "size":
if "marker" not in result:
result["marker"] = dict()
result["marker"]["size"] = g[v]
result["marker"]["sizemode"] = "area"
result["marker"]["sizeref"] = sizeref
mapping_labels[v_label] = "%{marker.size}"
elif k == "marginal_x":
if attr_name == "size":
if "marker" not in trace_patch:
trace_patch["marker"] = dict()
trace_patch["marker"]["size"] = trace_data[attr_value]
trace_patch["marker"]["sizemode"] = "area"
trace_patch["marker"]["sizeref"] = sizeref
mapping_labels[attr_label] = "%{marker.size}"
elif attr_name == "marginal_x":
if trace_spec.constructor == go.Histogram:
mapping_labels["count"] = "%{y}"
elif k == "marginal_y":
elif attr_name == "marginal_y":
if trace_spec.constructor == go.Histogram:
mapping_labels["count"] = "%{x}"
elif k == "trendline":
if v in ["ols", "lowess"] and args["x"] and args["y"] and len(g) > 1:
elif attr_name == "trendline":
if (
attr_value in ["ols", "lowess"]
and args["x"]
and args["y"]
and len(trace_data) > 1
):
import statsmodels.api as sm

# sorting is bad but trace_specs with "trendline" have no other attrs
g2 = g.sort_values(by=args["x"])
y = g2[args["y"]]
x = g2[args["x"]]
result["x"] = x
sorted_trace_data = trace_data.sort_values(by=args["x"])
y = sorted_trace_data[args["y"]]
x = sorted_trace_data[args["x"]]
trace_patch["x"] = x

if x.dtype.type == np.datetime64:
x = x.astype(int) / 10 ** 9 # convert to unix epoch seconds

if v == "lowess":
if attr_value == "lowess":
trendline = sm.nonparametric.lowess(y, x)
result["y"] = trendline[:, 1]
trace_patch["y"] = trendline[:, 1]
hover_header = "<b>LOWESS trendline</b><br><br>"
elif v == "ols":
elif attr_value == "ols":
fit_results = sm.OLS(y.values, sm.add_constant(x.values)).fit()
result["y"] = fit_results.predict()
trace_patch["y"] = fit_results.predict()
hover_header = "<b>OLS trendline</b><br>"
hover_header += "%s = %g * %s + %g<br>" % (
args["y"],
Expand All@@ -250,120 +255,127 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
mapping_labels[get_label(args, args["x"])] = "%{x}"
mapping_labels[get_label(args, args["y"])] = "%{y} <b>(trend)</b>"

elif k.startswith("error"):
error_xy = k[:7]
arr = "arrayminus" if k.endswith("minus") else "array"
if error_xy not in result:
result[error_xy] = {}
result[error_xy][arr] = g[v]
elif k == "custom_data":
result["customdata"] = g[v].values
custom_data_len = len(v) # number of custom data columns
elif k == "hover_name":
elif attr_name.startswith("error"):
error_xy = attr_name[:7]
arr = "arrayminus" if attr_name.endswith("minus") else "array"
if error_xy not in trace_patch:
trace_patch[error_xy] = {}
trace_patch[error_xy][arr] = trace_data[attr_value]
elif attr_name == "custom_data":
trace_patch["customdata"] = trace_data[attr_value].values
custom_data_len = len(attr_value) # number of custom data columns
elif attr_name == "hover_name":
if trace_spec.constructor not in [
go.Histogram,
go.Histogram2d,
go.Histogram2dContour,
]:
result["hovertext"] = g[v]
trace_patch["hovertext"] = trace_data[attr_value]
if hover_header == "":
hover_header = "<b>%{hovertext}</b><br><br>"
elif k == "hover_data":
elif attr_name == "hover_data":
if trace_spec.constructor not in [
go.Histogram,
go.Histogram2d,
go.Histogram2dContour,
]:
for col in v:
for col in attr_value:
try:
position = args["custom_data"].index(col)
except (ValueError, AttributeError, KeyError):
position = custom_data_len
custom_data_len += 1
if "customdata" in result:
result["customdata"] = np.hstack(
(result["customdata"], g[col].values[:, None])
if "customdata" in trace_patch:
trace_patch["customdata"] = np.hstack(
(
trace_patch["customdata"],
trace_data[col].values[:, None],
)
)
else:
result["customdata"] = g[col].values[:, None]
v_label_col = get_decorated_label(args, col, None)
mapping_labels[v_label_col] = "%%{customdata[%d]}" % (position)
elif k == "color":
trace_patch["customdata"] = trace_data[col].values[
:, None
]
attr_label_col = get_decorated_label(args, col, None)
mapping_labels[attr_label_col] = "%%{customdata[%d]}" % (
position
)
elif attr_name == "color":
if trace_spec.constructor in [go.Choropleth, go.Choroplethmapbox]:
result["z"] = g[v]
result["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{z}"
trace_patch["z"] = trace_data[attr_value]
trace_patch["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%{z}"
elif trace_spec.constructor in [
go.Sunburst,
go.Treemap,
go.Pie,
go.Funnelarea,
]:
if "marker" not in result:
result["marker"] = dict()
if "marker" not in trace_patch:
trace_patch["marker"] = dict()

if args.get("color_is_continuous"):
result["marker"]["colors"] = g[v]
result["marker"]["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{color}"
trace_patch["marker"]["colors"] = trace_data[attr_value]
trace_patch["marker"]["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%{color}"
else:
result["marker"]["colors"] = []
trace_patch["marker"]["colors"] = []
mapping = {}
for cat in g[v]:
for cat in trace_data[attr_value]:
if mapping.get(cat) is None:
mapping[cat] = args["color_discrete_sequence"][
len(mapping) % len(args["color_discrete_sequence"])
]
result["marker"]["colors"].append(mapping[cat])
trace_patch["marker"]["colors"].append(mapping[cat])
else:
colorable = "marker"
if trace_spec.constructor in [go.Parcats, go.Parcoords]:
colorable = "line"
if colorable not in result:
result[colorable] = dict()
result[colorable]["color"] = g[v]
result[colorable]["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%%{%s.color}" % colorable
elif k == "animation_group":
result["ids"] = g[v]
elif k == "locations":
result[k] = g[v]
mapping_labels[v_label] = "%{location}"
elif k == "values":
result[k] = g[v]
_label = "value" if v_label == "values" else v_label
if colorable not in trace_patch:
trace_patch[colorable] = dict()
trace_patch[colorable]["color"] = trace_data[attr_value]
trace_patch[colorable]["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%%{%s.color}" % colorable
elif attr_name == "animation_group":
trace_patch["ids"] = trace_data[attr_value]
elif attr_name == "locations":
trace_patch[attr_name] = trace_data[attr_value]
mapping_labels[attr_label] = "%{location}"
elif attr_name == "values":
trace_patch[attr_name] = trace_data[attr_value]
_label = "value" if attr_label == "values" else attr_label
mapping_labels[_label] = "%{value}"
elif k == "parents":
result[k] = g[v]
_label = "parent" if v_label == "parents" else v_label
elif attr_name == "parents":
trace_patch[attr_name] = trace_data[attr_value]
_label = "parent" if attr_label == "parents" else attr_label
mapping_labels[_label] = "%{parent}"
elif k == "ids":
result[k] = g[v]
_label = "id" if v_label == "ids" else v_label
elif attr_name == "ids":
trace_patch[attr_name] = trace_data[attr_value]
_label = "id" if attr_label == "ids" else attr_label
mapping_labels[_label] = "%{id}"
elif k == "names":
elif attr_name == "names":
if trace_spec.constructor in [
go.Sunburst,
go.Treemap,
go.Pie,
go.Funnelarea,
]:
result["labels"] = g[v]
_label = "label" if v_label == "names" else v_label
trace_patch["labels"] = trace_data[attr_value]
_label = "label" if attr_label == "names" else attr_label
mapping_labels[_label] = "%{label}"
else:
result[k] = g[v]
trace_patch[attr_name] = trace_data[attr_value]
else:
if v:
result[k] = g[v]
mapping_labels[v_label] = "%%{%s}" % k
if attr_value:
trace_patch[attr_name] = trace_data[attr_value]
mapping_labels[attr_label] = "%%{%s}" % attr_name
if trace_spec.constructor not in [
go.Parcoords,
go.Parcats,
]:
hover_lines = [k + "=" + v for k, v in mapping_labels.items()]
result["hovertemplate"] = hover_header + "<br>".join(hover_lines)
return result, fit_results
trace_patch["hovertemplate"] = hover_header + "<br>".join(hover_lines)
return trace_patch, fit_results


def configure_axes(args, constructor, fig, orders):
Expand DownExpand Up@@ -1015,8 +1027,8 @@ def _check_dataframe_all_leaves(df):
null_indices = np.nonzero(null_mask.any(axis=1).values)[0]
for null_row_index in null_indices:
row = null_mask.iloc[null_row_index]
indices = np.nonzero(row.values)[0]
if not row[indices[0] :].all():
i = np.nonzero(row.values)[0][0]
if not row[i:].all():
raise ValueError(
"None entries cannot have not-None children",
df_sorted.iloc[null_row_index],
Expand DownExpand Up@@ -1058,6 +1070,7 @@ def process_dataframe_hierarchy(args):
path = [new_col_name if x == col_name else x for x in path]
df[new_col_name] = series_to_copy
# ------------ Define aggregation functions --------------------------------

def aggfunc_discrete(x):
uniques = x.unique()
if len(uniques) == 1:
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 110 additions & 97 deletions packages/python/plotly/plotly/express/_core.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -137,7 +137,7 @@ def make_mapping(args, variable):
)


def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
"""Populates a dict with arguments to update trace

Parameters
Expand All@@ -147,7 +147,7 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
trace_spec : NamedTuple
which kind of trace to be used (has constructor, marginal etc.
attributes)
g : pandas DataFrame
trace_data : pandas DataFrame
data
mapping_labels : dict
to be used for hovertemplate
Expand All@@ -156,87 +156,92 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):

Returns
-------
result : dict
trace_patch : dict
dict to be used to update trace
fit_results : dict
fit information to be used for trendlines
"""
if "line_close" in args and args["line_close"]:
g = g.append(g.iloc[0])
result = trace_spec.trace_patch.copy() or {}
trace_data = trace_data.append(trace_data.iloc[0])
trace_patch = trace_spec.trace_patch.copy() or {}
fit_results = None
hover_header = ""
custom_data_len = 0
for k in trace_spec.attrs:
v = args[k]
v_label = get_decorated_label(args, v, k)
if k == "dimensions":
for attr_name in trace_spec.attrs:
attr_value = args[attr_name]
attr_label = get_decorated_label(args, attr_value, attr_name)
if attr_name == "dimensions":
dims = [
(name, column)
for (name, column) in g.iteritems()
if ((not v) or (name in v))
for (name, column) in trace_data.iteritems()
if ((not attr_value) or (name in attr_value))
and (
trace_spec.constructor != go.Parcoords
or args["data_frame"][name].dtype.kind in "bifc"
)
and (
trace_spec.constructor != go.Parcats
or (v is not None and name in v)
or (attr_value is not None and name in attr_value)
or len(args["data_frame"][name].unique())
<= args["dimensions_max_cardinality"]
)
]
result["dimensions"] = [
trace_patch["dimensions"] = [
dict(label=get_label(args, name), values=column.values)
for (name, column) in dims
]
if trace_spec.constructor == go.Splom:
for d in result["dimensions"]:
for d in trace_patch["dimensions"]:
d["axis"] = dict(matches=True)
mapping_labels["%{xaxis.title.text}"] = "%{x}"
mapping_labels["%{yaxis.title.text}"] = "%{y}"

elif (
v is not None
or (trace_spec.constructor == go.Histogram and k in ["x", "y"])
attr_value is not None
or (trace_spec.constructor == go.Histogram and attr_name in ["x", "y"])
or (
trace_spec.constructor in [go.Histogram2d, go.Histogram2dContour]
and k == "z"
and attr_name == "z"
)
):
if k == "size":
if "marker" not in result:
result["marker"] = dict()
result["marker"]["size"] = g[v]
result["marker"]["sizemode"] = "area"
result["marker"]["sizeref"] = sizeref
mapping_labels[v_label] = "%{marker.size}"
elif k == "marginal_x":
if attr_name == "size":
if "marker" not in trace_patch:
trace_patch["marker"] = dict()
trace_patch["marker"]["size"] = trace_data[attr_value]
trace_patch["marker"]["sizemode"] = "area"
trace_patch["marker"]["sizeref"] = sizeref
mapping_labels[attr_label] = "%{marker.size}"
elif attr_name == "marginal_x":
if trace_spec.constructor == go.Histogram:
mapping_labels["count"] = "%{y}"
elif k == "marginal_y":
elif attr_name == "marginal_y":
if trace_spec.constructor == go.Histogram:
mapping_labels["count"] = "%{x}"
elif k == "trendline":
if v in ["ols", "lowess"] and args["x"] and args["y"] and len(g) > 1:
elif attr_name == "trendline":
if (
attr_value in ["ols", "lowess"]
and args["x"]
and args["y"]
and len(trace_data) > 1
):
import statsmodels.api as sm

# sorting is bad but trace_specs with "trendline" have no other attrs
g2 = g.sort_values(by=args["x"])
y = g2[args["y"]]
x = g2[args["x"]]
result["x"] = x
sorted_trace_data = trace_data.sort_values(by=args["x"])
y = sorted_trace_data[args["y"]]
x = sorted_trace_data[args["x"]]
trace_patch["x"] = x

if x.dtype.type == np.datetime64:
x = x.astype(int) / 10 ** 9 # convert to unix epoch seconds

if v == "lowess":
if attr_value == "lowess":
trendline = sm.nonparametric.lowess(y, x)
result["y"] = trendline[:, 1]
trace_patch["y"] = trendline[:, 1]
hover_header = "<b>LOWESS trendline</b><br><br>"
elif v == "ols":
elif attr_value == "ols":
fit_results = sm.OLS(y.values, sm.add_constant(x.values)).fit()
result["y"] = fit_results.predict()
trace_patch["y"] = fit_results.predict()
hover_header = "<b>OLS trendline</b><br>"
hover_header += "%s = %g * %s + %g<br>" % (
args["y"],
Expand All@@ -250,120 +255,127 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
mapping_labels[get_label(args, args["x"])] = "%{x}"
mapping_labels[get_label(args, args["y"])] = "%{y} <b>(trend)</b>"

elif k.startswith("error"):
error_xy = k[:7]
arr = "arrayminus" if k.endswith("minus") else "array"
if error_xy not in result:
result[error_xy] = {}
result[error_xy][arr] = g[v]
elif k == "custom_data":
result["customdata"] = g[v].values
custom_data_len = len(v) # number of custom data columns
elif k == "hover_name":
elif attr_name.startswith("error"):
error_xy = attr_name[:7]
arr = "arrayminus" if attr_name.endswith("minus") else "array"
if error_xy not in trace_patch:
trace_patch[error_xy] = {}
trace_patch[error_xy][arr] = trace_data[attr_value]
elif attr_name == "custom_data":
trace_patch["customdata"] = trace_data[attr_value].values
custom_data_len = len(attr_value) # number of custom data columns
elif attr_name == "hover_name":
if trace_spec.constructor not in [
go.Histogram,
go.Histogram2d,
go.Histogram2dContour,
]:
result["hovertext"] = g[v]
trace_patch["hovertext"] = trace_data[attr_value]
if hover_header == "":
hover_header = "<b>%{hovertext}</b><br><br>"
elif k == "hover_data":
elif attr_name == "hover_data":
if trace_spec.constructor not in [
go.Histogram,
go.Histogram2d,
go.Histogram2dContour,
]:
for col in v:
for col in attr_value:
try:
position = args["custom_data"].index(col)
except (ValueError, AttributeError, KeyError):
position = custom_data_len
custom_data_len += 1
if "customdata" in result:
result["customdata"] = np.hstack(
(result["customdata"], g[col].values[:, None])
if "customdata" in trace_patch:
trace_patch["customdata"] = np.hstack(
(
trace_patch["customdata"],
trace_data[col].values[:, None],
)
)
else:
result["customdata"] = g[col].values[:, None]
v_label_col = get_decorated_label(args, col, None)
mapping_labels[v_label_col] = "%%{customdata[%d]}" % (position)
elif k == "color":
trace_patch["customdata"] = trace_data[col].values[
:, None
]
attr_label_col = get_decorated_label(args, col, None)
mapping_labels[attr_label_col] = "%%{customdata[%d]}" % (
position
)
elif attr_name == "color":
if trace_spec.constructor in [go.Choropleth, go.Choroplethmapbox]:
result["z"] = g[v]
result["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{z}"
trace_patch["z"] = trace_data[attr_value]
trace_patch["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%{z}"
elif trace_spec.constructor in [
go.Sunburst,
go.Treemap,
go.Pie,
go.Funnelarea,
]:
if "marker" not in result:
result["marker"] = dict()
if "marker" not in trace_patch:
trace_patch["marker"] = dict()

if args.get("color_is_continuous"):
result["marker"]["colors"] = g[v]
result["marker"]["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{color}"
trace_patch["marker"]["colors"] = trace_data[attr_value]
trace_patch["marker"]["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%{color}"
else:
result["marker"]["colors"] = []
trace_patch["marker"]["colors"] = []
mapping = {}
for cat in g[v]:
for cat in trace_data[attr_value]:
if mapping.get(cat) is None:
mapping[cat] = args["color_discrete_sequence"][
len(mapping) % len(args["color_discrete_sequence"])
]
result["marker"]["colors"].append(mapping[cat])
trace_patch["marker"]["colors"].append(mapping[cat])
else:
colorable = "marker"
if trace_spec.constructor in [go.Parcats, go.Parcoords]:
colorable = "line"
if colorable not in result:
result[colorable] = dict()
result[colorable]["color"] = g[v]
result[colorable]["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%%{%s.color}" % colorable
elif k == "animation_group":
result["ids"] = g[v]
elif k == "locations":
result[k] = g[v]
mapping_labels[v_label] = "%{location}"
elif k == "values":
result[k] = g[v]
_label = "value" if v_label == "values" else v_label
if colorable not in trace_patch:
trace_patch[colorable] = dict()
trace_patch[colorable]["color"] = trace_data[attr_value]
trace_patch[colorable]["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%%{%s.color}" % colorable
elif attr_name == "animation_group":
trace_patch["ids"] = trace_data[attr_value]
elif attr_name == "locations":
trace_patch[attr_name] = trace_data[attr_value]
mapping_labels[attr_label] = "%{location}"
elif attr_name == "values":
trace_patch[attr_name] = trace_data[attr_value]
_label = "value" if attr_label == "values" else attr_label
mapping_labels[_label] = "%{value}"
elif k == "parents":
result[k] = g[v]
_label = "parent" if v_label == "parents" else v_label
elif attr_name == "parents":
trace_patch[attr_name] = trace_data[attr_value]
_label = "parent" if attr_label == "parents" else attr_label
mapping_labels[_label] = "%{parent}"
elif k == "ids":
result[k] = g[v]
_label = "id" if v_label == "ids" else v_label
elif attr_name == "ids":
trace_patch[attr_name] = trace_data[attr_value]
_label = "id" if attr_label == "ids" else attr_label
mapping_labels[_label] = "%{id}"
elif k == "names":
elif attr_name == "names":
if trace_spec.constructor in [
go.Sunburst,
go.Treemap,
go.Pie,
go.Funnelarea,
]:
result["labels"] = g[v]
_label = "label" if v_label == "names" else v_label
trace_patch["labels"] = trace_data[attr_value]
_label = "label" if attr_label == "names" else attr_label
mapping_labels[_label] = "%{label}"
else:
result[k] = g[v]
trace_patch[attr_name] = trace_data[attr_value]
else:
if v:
result[k] = g[v]
mapping_labels[v_label] = "%%{%s}" % k
if attr_value:
trace_patch[attr_name] = trace_data[attr_value]
mapping_labels[attr_label] = "%%{%s}" % attr_name
if trace_spec.constructor not in [
go.Parcoords,
go.Parcats,
]:
hover_lines = [k + "=" + v for k, v in mapping_labels.items()]
result["hovertemplate"] = hover_header + "<br>".join(hover_lines)
return result, fit_results
trace_patch["hovertemplate"] = hover_header + "<br>".join(hover_lines)
return trace_patch, fit_results


def configure_axes(args, constructor, fig, orders):
Expand DownExpand Up@@ -1015,8 +1027,8 @@ def _check_dataframe_all_leaves(df):
null_indices = np.nonzero(null_mask.any(axis=1).values)[0]
for null_row_index in null_indices:
row = null_mask.iloc[null_row_index]
indices = np.nonzero(row.values)[0]
if not row[indices[0] :].all():
i = np.nonzero(row.values)[0][0]
if not row[i:].all():
raise ValueError(
"None entries cannot have not-None children",
df_sorted.iloc[null_row_index],
Expand DownExpand Up@@ -1058,6 +1070,7 @@ def process_dataframe_hierarchy(args):
path = [new_col_name if x == col_name else x for x in path]
df[new_col_name] = series_to_copy
# ------------ Define aggregation functions --------------------------------

def aggfunc_discrete(x):
uniques = x.unique()
if len(uniques) == 1:
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 110 additions & 97 deletions packages/python/plotly/plotly/express/_core.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -137,7 +137,7 @@ def make_mapping(args, variable):
)


def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
"""Populates a dict with arguments to update trace

Parameters
Expand All@@ -147,7 +147,7 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
trace_spec : NamedTuple
which kind of trace to be used (has constructor, marginal etc.
attributes)
g : pandas DataFrame
trace_data : pandas DataFrame
data
mapping_labels : dict
to be used for hovertemplate
Expand All@@ -156,87 +156,92 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):

Returns
-------
result : dict
trace_patch : dict
dict to be used to update trace
fit_results : dict
fit information to be used for trendlines
"""
if "line_close" in args and args["line_close"]:
g = g.append(g.iloc[0])
result = trace_spec.trace_patch.copy() or {}
trace_data = trace_data.append(trace_data.iloc[0])
trace_patch = trace_spec.trace_patch.copy() or {}
fit_results = None
hover_header = ""
custom_data_len = 0
for k in trace_spec.attrs:
v = args[k]
v_label = get_decorated_label(args, v, k)
if k == "dimensions":
for attr_name in trace_spec.attrs:
attr_value = args[attr_name]
attr_label = get_decorated_label(args, attr_value, attr_name)
if attr_name == "dimensions":
dims = [
(name, column)
for (name, column) in g.iteritems()
if ((not v) or (name in v))
for (name, column) in trace_data.iteritems()
if ((not attr_value) or (name in attr_value))
and (
trace_spec.constructor != go.Parcoords
or args["data_frame"][name].dtype.kind in "bifc"
)
and (
trace_spec.constructor != go.Parcats
or (v is not None and name in v)
or (attr_value is not None and name in attr_value)
or len(args["data_frame"][name].unique())
<= args["dimensions_max_cardinality"]
)
]
result["dimensions"] = [
trace_patch["dimensions"] = [
dict(label=get_label(args, name), values=column.values)
for (name, column) in dims
]
if trace_spec.constructor == go.Splom:
for d in result["dimensions"]:
for d in trace_patch["dimensions"]:
d["axis"] = dict(matches=True)
mapping_labels["%{xaxis.title.text}"] = "%{x}"
mapping_labels["%{yaxis.title.text}"] = "%{y}"

elif (
v is not None
or (trace_spec.constructor == go.Histogram and k in ["x", "y"])
attr_value is not None
or (trace_spec.constructor == go.Histogram and attr_name in ["x", "y"])
or (
trace_spec.constructor in [go.Histogram2d, go.Histogram2dContour]
and k == "z"
and attr_name == "z"
)
):
if k == "size":
if "marker" not in result:
result["marker"] = dict()
result["marker"]["size"] = g[v]
result["marker"]["sizemode"] = "area"
result["marker"]["sizeref"] = sizeref
mapping_labels[v_label] = "%{marker.size}"
elif k == "marginal_x":
if attr_name == "size":
if "marker" not in trace_patch:
trace_patch["marker"] = dict()
trace_patch["marker"]["size"] = trace_data[attr_value]
trace_patch["marker"]["sizemode"] = "area"
trace_patch["marker"]["sizeref"] = sizeref
mapping_labels[attr_label] = "%{marker.size}"
elif attr_name == "marginal_x":
if trace_spec.constructor == go.Histogram:
mapping_labels["count"] = "%{y}"
elif k == "marginal_y":
elif attr_name == "marginal_y":
if trace_spec.constructor == go.Histogram:
mapping_labels["count"] = "%{x}"
elif k == "trendline":
if v in ["ols", "lowess"] and args["x"] and args["y"] and len(g) > 1:
elif attr_name == "trendline":
if (
attr_value in ["ols", "lowess"]
and args["x"]
and args["y"]
and len(trace_data) > 1
):
import statsmodels.api as sm

# sorting is bad but trace_specs with "trendline" have no other attrs
g2 = g.sort_values(by=args["x"])
y = g2[args["y"]]
x = g2[args["x"]]
result["x"] = x
sorted_trace_data = trace_data.sort_values(by=args["x"])
y = sorted_trace_data[args["y"]]
x = sorted_trace_data[args["x"]]
trace_patch["x"] = x

if x.dtype.type == np.datetime64:
x = x.astype(int) / 10 ** 9 # convert to unix epoch seconds

if v == "lowess":
if attr_value == "lowess":
trendline = sm.nonparametric.lowess(y, x)
result["y"] = trendline[:, 1]
trace_patch["y"] = trendline[:, 1]
hover_header = "<b>LOWESS trendline</b><br><br>"
elif v == "ols":
elif attr_value == "ols":
fit_results = sm.OLS(y.values, sm.add_constant(x.values)).fit()
result["y"] = fit_results.predict()
trace_patch["y"] = fit_results.predict()
hover_header = "<b>OLS trendline</b><br>"
hover_header += "%s = %g * %s + %g<br>" % (
args["y"],
Expand All@@ -250,120 +255,127 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
mapping_labels[get_label(args, args["x"])] = "%{x}"
mapping_labels[get_label(args, args["y"])] = "%{y} <b>(trend)</b>"

elif k.startswith("error"):
error_xy = k[:7]
arr = "arrayminus" if k.endswith("minus") else "array"
if error_xy not in result:
result[error_xy] = {}
result[error_xy][arr] = g[v]
elif k == "custom_data":
result["customdata"] = g[v].values
custom_data_len = len(v) # number of custom data columns
elif k == "hover_name":
elif attr_name.startswith("error"):
error_xy = attr_name[:7]
arr = "arrayminus" if attr_name.endswith("minus") else "array"
if error_xy not in trace_patch:
trace_patch[error_xy] = {}
trace_patch[error_xy][arr] = trace_data[attr_value]
elif attr_name == "custom_data":
trace_patch["customdata"] = trace_data[attr_value].values
custom_data_len = len(attr_value) # number of custom data columns
elif attr_name == "hover_name":
if trace_spec.constructor not in [
go.Histogram,
go.Histogram2d,
go.Histogram2dContour,
]:
result["hovertext"] = g[v]
trace_patch["hovertext"] = trace_data[attr_value]
if hover_header == "":
hover_header = "<b>%{hovertext}</b><br><br>"
elif k == "hover_data":
elif attr_name == "hover_data":
if trace_spec.constructor not in [
go.Histogram,
go.Histogram2d,
go.Histogram2dContour,
]:
for col in v:
for col in attr_value:
try:
position = args["custom_data"].index(col)
except (ValueError, AttributeError, KeyError):
position = custom_data_len
custom_data_len += 1
if "customdata" in result:
result["customdata"] = np.hstack(
(result["customdata"], g[col].values[:, None])
if "customdata" in trace_patch:
trace_patch["customdata"] = np.hstack(
(
trace_patch["customdata"],
trace_data[col].values[:, None],
)
)
else:
result["customdata"] = g[col].values[:, None]
v_label_col = get_decorated_label(args, col, None)
mapping_labels[v_label_col] = "%%{customdata[%d]}" % (position)
elif k == "color":
trace_patch["customdata"] = trace_data[col].values[
:, None
]
attr_label_col = get_decorated_label(args, col, None)
mapping_labels[attr_label_col] = "%%{customdata[%d]}" % (
position
)
elif attr_name == "color":
if trace_spec.constructor in [go.Choropleth, go.Choroplethmapbox]:
result["z"] = g[v]
result["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{z}"
trace_patch["z"] = trace_data[attr_value]
trace_patch["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%{z}"
elif trace_spec.constructor in [
go.Sunburst,
go.Treemap,
go.Pie,
go.Funnelarea,
]:
if "marker" not in result:
result["marker"] = dict()
if "marker" not in trace_patch:
trace_patch["marker"] = dict()

if args.get("color_is_continuous"):
result["marker"]["colors"] = g[v]
result["marker"]["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{color}"
trace_patch["marker"]["colors"] = trace_data[attr_value]
trace_patch["marker"]["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%{color}"
else:
result["marker"]["colors"] = []
trace_patch["marker"]["colors"] = []
mapping = {}
for cat in g[v]:
for cat in trace_data[attr_value]:
if mapping.get(cat) is None:
mapping[cat] = args["color_discrete_sequence"][
len(mapping) % len(args["color_discrete_sequence"])
]
result["marker"]["colors"].append(mapping[cat])
trace_patch["marker"]["colors"].append(mapping[cat])
else:
colorable = "marker"
if trace_spec.constructor in [go.Parcats, go.Parcoords]:
colorable = "line"
if colorable not in result:
result[colorable] = dict()
result[colorable]["color"] = g[v]
result[colorable]["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%%{%s.color}" % colorable
elif k == "animation_group":
result["ids"] = g[v]
elif k == "locations":
result[k] = g[v]
mapping_labels[v_label] = "%{location}"
elif k == "values":
result[k] = g[v]
_label = "value" if v_label == "values" else v_label
if colorable not in trace_patch:
trace_patch[colorable] = dict()
trace_patch[colorable]["color"] = trace_data[attr_value]
trace_patch[colorable]["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%%{%s.color}" % colorable
elif attr_name == "animation_group":
trace_patch["ids"] = trace_data[attr_value]
elif attr_name == "locations":
trace_patch[attr_name] = trace_data[attr_value]
mapping_labels[attr_label] = "%{location}"
elif attr_name == "values":
trace_patch[attr_name] = trace_data[attr_value]
_label = "value" if attr_label == "values" else attr_label
mapping_labels[_label] = "%{value}"
elif k == "parents":
result[k] = g[v]
_label = "parent" if v_label == "parents" else v_label
elif attr_name == "parents":
trace_patch[attr_name] = trace_data[attr_value]
_label = "parent" if attr_label == "parents" else attr_label
mapping_labels[_label] = "%{parent}"
elif k == "ids":
result[k] = g[v]
_label = "id" if v_label == "ids" else v_label
elif attr_name == "ids":
trace_patch[attr_name] = trace_data[attr_value]
_label = "id" if attr_label == "ids" else attr_label
mapping_labels[_label] = "%{id}"
elif k == "names":
elif attr_name == "names":
if trace_spec.constructor in [
go.Sunburst,
go.Treemap,
go.Pie,
go.Funnelarea,
]:
result["labels"] = g[v]
_label = "label" if v_label == "names" else v_label
trace_patch["labels"] = trace_data[attr_value]
_label = "label" if attr_label == "names" else attr_label
mapping_labels[_label] = "%{label}"
else:
result[k] = g[v]
trace_patch[attr_name] = trace_data[attr_value]
else:
if v:
result[k] = g[v]
mapping_labels[v_label] = "%%{%s}" % k
if attr_value:
trace_patch[attr_name] = trace_data[attr_value]
mapping_labels[attr_label] = "%%{%s}" % attr_name
if trace_spec.constructor not in [
go.Parcoords,
go.Parcats,
]:
hover_lines = [k + "=" + v for k, v in mapping_labels.items()]
result["hovertemplate"] = hover_header + "<br>".join(hover_lines)
return result, fit_results
trace_patch["hovertemplate"] = hover_header + "<br>".join(hover_lines)
return trace_patch, fit_results


def configure_axes(args, constructor, fig, orders):
Expand DownExpand Up@@ -1015,8 +1027,8 @@ def _check_dataframe_all_leaves(df):
null_indices = np.nonzero(null_mask.any(axis=1).values)[0]
for null_row_index in null_indices:
row = null_mask.iloc[null_row_index]
indices = np.nonzero(row.values)[0]
if not row[indices[0] :].all():
i = np.nonzero(row.values)[0][0]
if not row[i:].all():
raise ValueError(
"None entries cannot have not-None children",
df_sorted.iloc[null_row_index],
Expand DownExpand Up@@ -1058,6 +1070,7 @@ def process_dataframe_hierarchy(args):
path = [new_col_name if x == col_name else x for x in path]
df[new_col_name] = series_to_copy
# ------------ Define aggregation functions --------------------------------

def aggfunc_discrete(x):
uniques = x.unique()
if len(uniques) == 1:
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 110 additions & 97 deletions packages/python/plotly/plotly/express/_core.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -137,7 +137,7 @@ def make_mapping(args, variable):
)


def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
"""Populates a dict with arguments to update trace

Parameters
Expand All@@ -147,7 +147,7 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
trace_spec : NamedTuple
which kind of trace to be used (has constructor, marginal etc.
attributes)
g : pandas DataFrame
trace_data : pandas DataFrame
data
mapping_labels : dict
to be used for hovertemplate
Expand All@@ -156,87 +156,92 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):

Returns
-------
result : dict
trace_patch : dict
dict to be used to update trace
fit_results : dict
fit information to be used for trendlines
"""
if "line_close" in args and args["line_close"]:
g = g.append(g.iloc[0])
result = trace_spec.trace_patch.copy() or {}
trace_data = trace_data.append(trace_data.iloc[0])
trace_patch = trace_spec.trace_patch.copy() or {}
fit_results = None
hover_header = ""
custom_data_len = 0
for k in trace_spec.attrs:
v = args[k]
v_label = get_decorated_label(args, v, k)
if k == "dimensions":
for attr_name in trace_spec.attrs:
attr_value = args[attr_name]
attr_label = get_decorated_label(args, attr_value, attr_name)
if attr_name == "dimensions":
dims = [
(name, column)
for (name, column) in g.iteritems()
if ((not v) or (name in v))
for (name, column) in trace_data.iteritems()
if ((not attr_value) or (name in attr_value))
and (
trace_spec.constructor != go.Parcoords
or args["data_frame"][name].dtype.kind in "bifc"
)
and (
trace_spec.constructor != go.Parcats
or (v is not None and name in v)
or (attr_value is not None and name in attr_value)
or len(args["data_frame"][name].unique())
<= args["dimensions_max_cardinality"]
)
]
result["dimensions"] = [
trace_patch["dimensions"] = [
dict(label=get_label(args, name), values=column.values)
for (name, column) in dims
]
if trace_spec.constructor == go.Splom:
for d in result["dimensions"]:
for d in trace_patch["dimensions"]:
d["axis"] = dict(matches=True)
mapping_labels["%{xaxis.title.text}"] = "%{x}"
mapping_labels["%{yaxis.title.text}"] = "%{y}"

elif (
v is not None
or (trace_spec.constructor == go.Histogram and k in ["x", "y"])
attr_value is not None
or (trace_spec.constructor == go.Histogram and attr_name in ["x", "y"])
or (
trace_spec.constructor in [go.Histogram2d, go.Histogram2dContour]
and k == "z"
and attr_name == "z"
)
):
if k == "size":
if "marker" not in result:
result["marker"] = dict()
result["marker"]["size"] = g[v]
result["marker"]["sizemode"] = "area"
result["marker"]["sizeref"] = sizeref
mapping_labels[v_label] = "%{marker.size}"
elif k == "marginal_x":
if attr_name == "size":
if "marker" not in trace_patch:
trace_patch["marker"] = dict()
trace_patch["marker"]["size"] = trace_data[attr_value]
trace_patch["marker"]["sizemode"] = "area"
trace_patch["marker"]["sizeref"] = sizeref
mapping_labels[attr_label] = "%{marker.size}"
elif attr_name == "marginal_x":
if trace_spec.constructor == go.Histogram:
mapping_labels["count"] = "%{y}"
elif k == "marginal_y":
elif attr_name == "marginal_y":
if trace_spec.constructor == go.Histogram:
mapping_labels["count"] = "%{x}"
elif k == "trendline":
if v in ["ols", "lowess"] and args["x"] and args["y"] and len(g) > 1:
elif attr_name == "trendline":
if (
attr_value in ["ols", "lowess"]
and args["x"]
and args["y"]
and len(trace_data) > 1
):
import statsmodels.api as sm

# sorting is bad but trace_specs with "trendline" have no other attrs
g2 = g.sort_values(by=args["x"])
y = g2[args["y"]]
x = g2[args["x"]]
result["x"] = x
sorted_trace_data = trace_data.sort_values(by=args["x"])
y = sorted_trace_data[args["y"]]
x = sorted_trace_data[args["x"]]
trace_patch["x"] = x

if x.dtype.type == np.datetime64:
x = x.astype(int) / 10 ** 9 # convert to unix epoch seconds

if v == "lowess":
if attr_value == "lowess":
trendline = sm.nonparametric.lowess(y, x)
result["y"] = trendline[:, 1]
trace_patch["y"] = trendline[:, 1]
hover_header = "<b>LOWESS trendline</b><br><br>"
elif v == "ols":
elif attr_value == "ols":
fit_results = sm.OLS(y.values, sm.add_constant(x.values)).fit()
result["y"] = fit_results.predict()
trace_patch["y"] = fit_results.predict()
hover_header = "<b>OLS trendline</b><br>"
hover_header += "%s = %g * %s + %g<br>" % (
args["y"],
Expand All@@ -250,120 +255,127 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
mapping_labels[get_label(args, args["x"])] = "%{x}"
mapping_labels[get_label(args, args["y"])] = "%{y} <b>(trend)</b>"

elif k.startswith("error"):
error_xy = k[:7]
arr = "arrayminus" if k.endswith("minus") else "array"
if error_xy not in result:
result[error_xy] = {}
result[error_xy][arr] = g[v]
elif k == "custom_data":
result["customdata"] = g[v].values
custom_data_len = len(v) # number of custom data columns
elif k == "hover_name":
elif attr_name.startswith("error"):
error_xy = attr_name[:7]
arr = "arrayminus" if attr_name.endswith("minus") else "array"
if error_xy not in trace_patch:
trace_patch[error_xy] = {}
trace_patch[error_xy][arr] = trace_data[attr_value]
elif attr_name == "custom_data":
trace_patch["customdata"] = trace_data[attr_value].values
custom_data_len = len(attr_value) # number of custom data columns
elif attr_name == "hover_name":
if trace_spec.constructor not in [
go.Histogram,
go.Histogram2d,
go.Histogram2dContour,
]:
result["hovertext"] = g[v]
trace_patch["hovertext"] = trace_data[attr_value]
if hover_header == "":
hover_header = "<b>%{hovertext}</b><br><br>"
elif k == "hover_data":
elif attr_name == "hover_data":
if trace_spec.constructor not in [
go.Histogram,
go.Histogram2d,
go.Histogram2dContour,
]:
for col in v:
for col in attr_value:
try:
position = args["custom_data"].index(col)
except (ValueError, AttributeError, KeyError):
position = custom_data_len
custom_data_len += 1
if "customdata" in result:
result["customdata"] = np.hstack(
(result["customdata"], g[col].values[:, None])
if "customdata" in trace_patch:
trace_patch["customdata"] = np.hstack(
(
trace_patch["customdata"],
trace_data[col].values[:, None],
)
)
else:
result["customdata"] = g[col].values[:, None]
v_label_col = get_decorated_label(args, col, None)
mapping_labels[v_label_col] = "%%{customdata[%d]}" % (position)
elif k == "color":
trace_patch["customdata"] = trace_data[col].values[
:, None
]
attr_label_col = get_decorated_label(args, col, None)
mapping_labels[attr_label_col] = "%%{customdata[%d]}" % (
position
)
elif attr_name == "color":
if trace_spec.constructor in [go.Choropleth, go.Choroplethmapbox]:
result["z"] = g[v]
result["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{z}"
trace_patch["z"] = trace_data[attr_value]
trace_patch["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%{z}"
elif trace_spec.constructor in [
go.Sunburst,
go.Treemap,
go.Pie,
go.Funnelarea,
]:
if "marker" not in result:
result["marker"] = dict()
if "marker" not in trace_patch:
trace_patch["marker"] = dict()

if args.get("color_is_continuous"):
result["marker"]["colors"] = g[v]
result["marker"]["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{color}"
trace_patch["marker"]["colors"] = trace_data[attr_value]
trace_patch["marker"]["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%{color}"
else:
result["marker"]["colors"] = []
trace_patch["marker"]["colors"] = []
mapping = {}
for cat in g[v]:
for cat in trace_data[attr_value]:
if mapping.get(cat) is None:
mapping[cat] = args["color_discrete_sequence"][
len(mapping) % len(args["color_discrete_sequence"])
]
result["marker"]["colors"].append(mapping[cat])
trace_patch["marker"]["colors"].append(mapping[cat])
else:
colorable = "marker"
if trace_spec.constructor in [go.Parcats, go.Parcoords]:
colorable = "line"
if colorable not in result:
result[colorable] = dict()
result[colorable]["color"] = g[v]
result[colorable]["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%%{%s.color}" % colorable
elif k == "animation_group":
result["ids"] = g[v]
elif k == "locations":
result[k] = g[v]
mapping_labels[v_label] = "%{location}"
elif k == "values":
result[k] = g[v]
_label = "value" if v_label == "values" else v_label
if colorable not in trace_patch:
trace_patch[colorable] = dict()
trace_patch[colorable]["color"] = trace_data[attr_value]
trace_patch[colorable]["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%%{%s.color}" % colorable
elif attr_name == "animation_group":
trace_patch["ids"] = trace_data[attr_value]
elif attr_name == "locations":
trace_patch[attr_name] = trace_data[attr_value]
mapping_labels[attr_label] = "%{location}"
elif attr_name == "values":
trace_patch[attr_name] = trace_data[attr_value]
_label = "value" if attr_label == "values" else attr_label
mapping_labels[_label] = "%{value}"
elif k == "parents":
result[k] = g[v]
_label = "parent" if v_label == "parents" else v_label
elif attr_name == "parents":
trace_patch[attr_name] = trace_data[attr_value]
_label = "parent" if attr_label == "parents" else attr_label
mapping_labels[_label] = "%{parent}"
elif k == "ids":
result[k] = g[v]
_label = "id" if v_label == "ids" else v_label
elif attr_name == "ids":
trace_patch[attr_name] = trace_data[attr_value]
_label = "id" if attr_label == "ids" else attr_label
mapping_labels[_label] = "%{id}"
elif k == "names":
elif attr_name == "names":
if trace_spec.constructor in [
go.Sunburst,
go.Treemap,
go.Pie,
go.Funnelarea,
]:
result["labels"] = g[v]
_label = "label" if v_label == "names" else v_label
trace_patch["labels"] = trace_data[attr_value]
_label = "label" if attr_label == "names" else attr_label
mapping_labels[_label] = "%{label}"
else:
result[k] = g[v]
trace_patch[attr_name] = trace_data[attr_value]
else:
if v:
result[k] = g[v]
mapping_labels[v_label] = "%%{%s}" % k
if attr_value:
trace_patch[attr_name] = trace_data[attr_value]
mapping_labels[attr_label] = "%%{%s}" % attr_name
if trace_spec.constructor not in [
go.Parcoords,
go.Parcats,
]:
hover_lines = [k + "=" + v for k, v in mapping_labels.items()]
result["hovertemplate"] = hover_header + "<br>".join(hover_lines)
return result, fit_results
trace_patch["hovertemplate"] = hover_header + "<br>".join(hover_lines)
return trace_patch, fit_results


def configure_axes(args, constructor, fig, orders):
Expand DownExpand Up@@ -1015,8 +1027,8 @@ def _check_dataframe_all_leaves(df):
null_indices = np.nonzero(null_mask.any(axis=1).values)[0]
for null_row_index in null_indices:
row = null_mask.iloc[null_row_index]
indices = np.nonzero(row.values)[0]
if not row[indices[0] :].all():
i = np.nonzero(row.values)[0][0]
if not row[i:].all():
raise ValueError(
"None entries cannot have not-None children",
df_sorted.iloc[null_row_index],
Expand DownExpand Up@@ -1058,6 +1070,7 @@ def process_dataframe_hierarchy(args):
path = [new_col_name if x == col_name else x for x in path]
df[new_col_name] = series_to_copy
# ------------ Define aggregation functions --------------------------------

def aggfunc_discrete(x):
uniques = x.unique()
if len(uniques) == 1:
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 110 additions & 97 deletions packages/python/plotly/plotly/express/_core.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -137,7 +137,7 @@ def make_mapping(args, variable):
)


def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
"""Populates a dict with arguments to update trace

Parameters
Expand All@@ -147,7 +147,7 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
trace_spec : NamedTuple
which kind of trace to be used (has constructor, marginal etc.
attributes)
g : pandas DataFrame
trace_data : pandas DataFrame
data
mapping_labels : dict
to be used for hovertemplate
Expand All@@ -156,87 +156,92 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):

Returns
-------
result : dict
trace_patch : dict
dict to be used to update trace
fit_results : dict
fit information to be used for trendlines
"""
if "line_close" in args and args["line_close"]:
g = g.append(g.iloc[0])
result = trace_spec.trace_patch.copy() or {}
trace_data = trace_data.append(trace_data.iloc[0])
trace_patch = trace_spec.trace_patch.copy() or {}
fit_results = None
hover_header = ""
custom_data_len = 0
for k in trace_spec.attrs:
v = args[k]
v_label = get_decorated_label(args, v, k)
if k == "dimensions":
for attr_name in trace_spec.attrs:
attr_value = args[attr_name]
attr_label = get_decorated_label(args, attr_value, attr_name)
if attr_name == "dimensions":
dims = [
(name, column)
for (name, column) in g.iteritems()
if ((not v) or (name in v))
for (name, column) in trace_data.iteritems()
if ((not attr_value) or (name in attr_value))
and (
trace_spec.constructor != go.Parcoords
or args["data_frame"][name].dtype.kind in "bifc"
)
and (
trace_spec.constructor != go.Parcats
or (v is not None and name in v)
or (attr_value is not None and name in attr_value)
or len(args["data_frame"][name].unique())
<= args["dimensions_max_cardinality"]
)
]
result["dimensions"] = [
trace_patch["dimensions"] = [
dict(label=get_label(args, name), values=column.values)
for (name, column) in dims
]
if trace_spec.constructor == go.Splom:
for d in result["dimensions"]:
for d in trace_patch["dimensions"]:
d["axis"] = dict(matches=True)
mapping_labels["%{xaxis.title.text}"] = "%{x}"
mapping_labels["%{yaxis.title.text}"] = "%{y}"

elif (
v is not None
or (trace_spec.constructor == go.Histogram and k in ["x", "y"])
attr_value is not None
or (trace_spec.constructor == go.Histogram and attr_name in ["x", "y"])
or (
trace_spec.constructor in [go.Histogram2d, go.Histogram2dContour]
and k == "z"
and attr_name == "z"
)
):
if k == "size":
if "marker" not in result:
result["marker"] = dict()
result["marker"]["size"] = g[v]
result["marker"]["sizemode"] = "area"
result["marker"]["sizeref"] = sizeref
mapping_labels[v_label] = "%{marker.size}"
elif k == "marginal_x":
if attr_name == "size":
if "marker" not in trace_patch:
trace_patch["marker"] = dict()
trace_patch["marker"]["size"] = trace_data[attr_value]
trace_patch["marker"]["sizemode"] = "area"
trace_patch["marker"]["sizeref"] = sizeref
mapping_labels[attr_label] = "%{marker.size}"
elif attr_name == "marginal_x":
if trace_spec.constructor == go.Histogram:
mapping_labels["count"] = "%{y}"
elif k == "marginal_y":
elif attr_name == "marginal_y":
if trace_spec.constructor == go.Histogram:
mapping_labels["count"] = "%{x}"
elif k == "trendline":
if v in ["ols", "lowess"] and args["x"] and args["y"] and len(g) > 1:
elif attr_name == "trendline":
if (
attr_value in ["ols", "lowess"]
and args["x"]
and args["y"]
and len(trace_data) > 1
):
import statsmodels.api as sm

# sorting is bad but trace_specs with "trendline" have no other attrs
g2 = g.sort_values(by=args["x"])
y = g2[args["y"]]
x = g2[args["x"]]
result["x"] = x
sorted_trace_data = trace_data.sort_values(by=args["x"])
y = sorted_trace_data[args["y"]]
x = sorted_trace_data[args["x"]]
trace_patch["x"] = x

if x.dtype.type == np.datetime64:
x = x.astype(int) / 10 ** 9 # convert to unix epoch seconds

if v == "lowess":
if attr_value == "lowess":
trendline = sm.nonparametric.lowess(y, x)
result["y"] = trendline[:, 1]
trace_patch["y"] = trendline[:, 1]
hover_header = "<b>LOWESS trendline</b><br><br>"
elif v == "ols":
elif attr_value == "ols":
fit_results = sm.OLS(y.values, sm.add_constant(x.values)).fit()
result["y"] = fit_results.predict()
trace_patch["y"] = fit_results.predict()
hover_header = "<b>OLS trendline</b><br>"
hover_header += "%s = %g * %s + %g<br>" % (
args["y"],
Expand All@@ -250,120 +255,127 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref):
mapping_labels[get_label(args, args["x"])] = "%{x}"
mapping_labels[get_label(args, args["y"])] = "%{y} <b>(trend)</b>"

elif k.startswith("error"):
error_xy = k[:7]
arr = "arrayminus" if k.endswith("minus") else "array"
if error_xy not in result:
result[error_xy] = {}
result[error_xy][arr] = g[v]
elif k == "custom_data":
result["customdata"] = g[v].values
custom_data_len = len(v) # number of custom data columns
elif k == "hover_name":
elif attr_name.startswith("error"):
error_xy = attr_name[:7]
arr = "arrayminus" if attr_name.endswith("minus") else "array"
if error_xy not in trace_patch:
trace_patch[error_xy] = {}
trace_patch[error_xy][arr] = trace_data[attr_value]
elif attr_name == "custom_data":
trace_patch["customdata"] = trace_data[attr_value].values
custom_data_len = len(attr_value) # number of custom data columns
elif attr_name == "hover_name":
if trace_spec.constructor not in [
go.Histogram,
go.Histogram2d,
go.Histogram2dContour,
]:
result["hovertext"] = g[v]
trace_patch["hovertext"] = trace_data[attr_value]
if hover_header == "":
hover_header = "<b>%{hovertext}</b><br><br>"
elif k == "hover_data":
elif attr_name == "hover_data":
if trace_spec.constructor not in [
go.Histogram,
go.Histogram2d,
go.Histogram2dContour,
]:
for col in v:
for col in attr_value:
try:
position = args["custom_data"].index(col)
except (ValueError, AttributeError, KeyError):
position = custom_data_len
custom_data_len += 1
if "customdata" in result:
result["customdata"] = np.hstack(
(result["customdata"], g[col].values[:, None])
if "customdata" in trace_patch:
trace_patch["customdata"] = np.hstack(
(
trace_patch["customdata"],
trace_data[col].values[:, None],
)
)
else:
result["customdata"] = g[col].values[:, None]
v_label_col = get_decorated_label(args, col, None)
mapping_labels[v_label_col] = "%%{customdata[%d]}" % (position)
elif k == "color":
trace_patch["customdata"] = trace_data[col].values[
:, None
]
attr_label_col = get_decorated_label(args, col, None)
mapping_labels[attr_label_col] = "%%{customdata[%d]}" % (
position
)
elif attr_name == "color":
if trace_spec.constructor in [go.Choropleth, go.Choroplethmapbox]:
result["z"] = g[v]
result["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{z}"
trace_patch["z"] = trace_data[attr_value]
trace_patch["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%{z}"
elif trace_spec.constructor in [
go.Sunburst,
go.Treemap,
go.Pie,
go.Funnelarea,
]:
if "marker" not in result:
result["marker"] = dict()
if "marker" not in trace_patch:
trace_patch["marker"] = dict()

if args.get("color_is_continuous"):
result["marker"]["colors"] = g[v]
result["marker"]["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%{color}"
trace_patch["marker"]["colors"] = trace_data[attr_value]
trace_patch["marker"]["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%{color}"
else:
result["marker"]["colors"] = []
trace_patch["marker"]["colors"] = []
mapping = {}
for cat in g[v]:
for cat in trace_data[attr_value]:
if mapping.get(cat) is None:
mapping[cat] = args["color_discrete_sequence"][
len(mapping) % len(args["color_discrete_sequence"])
]
result["marker"]["colors"].append(mapping[cat])
trace_patch["marker"]["colors"].append(mapping[cat])
else:
colorable = "marker"
if trace_spec.constructor in [go.Parcats, go.Parcoords]:
colorable = "line"
if colorable not in result:
result[colorable] = dict()
result[colorable]["color"] = g[v]
result[colorable]["coloraxis"] = "coloraxis1"
mapping_labels[v_label] = "%%{%s.color}" % colorable
elif k == "animation_group":
result["ids"] = g[v]
elif k == "locations":
result[k] = g[v]
mapping_labels[v_label] = "%{location}"
elif k == "values":
result[k] = g[v]
_label = "value" if v_label == "values" else v_label
if colorable not in trace_patch:
trace_patch[colorable] = dict()
trace_patch[colorable]["color"] = trace_data[attr_value]
trace_patch[colorable]["coloraxis"] = "coloraxis1"
mapping_labels[attr_label] = "%%{%s.color}" % colorable
elif attr_name == "animation_group":
trace_patch["ids"] = trace_data[attr_value]
elif attr_name == "locations":
trace_patch[attr_name] = trace_data[attr_value]
mapping_labels[attr_label] = "%{location}"
elif attr_name == "values":
trace_patch[attr_name] = trace_data[attr_value]
_label = "value" if attr_label == "values" else attr_label
mapping_labels[_label] = "%{value}"
elif k == "parents":
result[k] = g[v]
_label = "parent" if v_label == "parents" else v_label
elif attr_name == "parents":
trace_patch[attr_name] = trace_data[attr_value]
_label = "parent" if attr_label == "parents" else attr_label
mapping_labels[_label] = "%{parent}"
elif k == "ids":
result[k] = g[v]
_label = "id" if v_label == "ids" else v_label
elif attr_name == "ids":
trace_patch[attr_name] = trace_data[attr_value]
_label = "id" if attr_label == "ids" else attr_label
mapping_labels[_label] = "%{id}"
elif k == "names":
elif attr_name == "names":
if trace_spec.constructor in [
go.Sunburst,
go.Treemap,
go.Pie,
go.Funnelarea,
]:
result["labels"] = g[v]
_label = "label" if v_label == "names" else v_label
trace_patch["labels"] = trace_data[attr_value]
_label = "label" if attr_label == "names" else attr_label
mapping_labels[_label] = "%{label}"
else:
result[k] = g[v]
trace_patch[attr_name] = trace_data[attr_value]
else:
if v:
result[k] = g[v]
mapping_labels[v_label] = "%%{%s}" % k
if attr_value:
trace_patch[attr_name] = trace_data[attr_value]
mapping_labels[attr_label] = "%%{%s}" % attr_name
if trace_spec.constructor not in [
go.Parcoords,
go.Parcats,
]:
hover_lines = [k + "=" + v for k, v in mapping_labels.items()]
result["hovertemplate"] = hover_header + "<br>".join(hover_lines)
return result, fit_results
trace_patch["hovertemplate"] = hover_header + "<br>".join(hover_lines)
return trace_patch, fit_results


def configure_axes(args, constructor, fig, orders):
Expand DownExpand Up@@ -1015,8 +1027,8 @@ def _check_dataframe_all_leaves(df):
null_indices = np.nonzero(null_mask.any(axis=1).values)[0]
for null_row_index in null_indices:
row = null_mask.iloc[null_row_index]
indices = np.nonzero(row.values)[0]
if not row[indices[0] :].all():
i = np.nonzero(row.values)[0][0]
if not row[i:].all():
raise ValueError(
"None entries cannot have not-None children",
df_sorted.iloc[null_row_index],
Expand DownExpand Up@@ -1058,6 +1070,7 @@ def process_dataframe_hierarchy(args):
path = [new_col_name if x == col_name else x for x in path]
df[new_col_name] = series_to_copy
# ------------ Define aggregation functions --------------------------------

def aggfunc_discrete(x):
uniques = x.unique()
if len(uniques) == 1:
Expand Down