From d7fc1bc967f67edb101c05eb62f632d4c9275515 Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Tue, 28 Jan 2020 15:19:42 -0500 Subject: [PATCH 1/6] tweaks to make flake8 happy --- packages/python/plotly/plotly/express/_core.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index e94a79d3954..a327f10231e 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -1015,8 +1015,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], @@ -1058,6 +1058,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: From e9fa00de208fe3fda34289b2152ac5de4058c3c9 Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Tue, 28 Jan 2020 15:24:27 -0500 Subject: [PATCH 2/6] g -> trace_data --- .../python/plotly/plotly/express/_core.py | 60 +++++++++++-------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index a327f10231e..8e8d2b2ecb6 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -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 @@ -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 @@ -162,7 +162,7 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref): fit information to be used for trendlines """ if "line_close" in args and args["line_close"]: - g = g.append(g.iloc[0]) + trace_data = trace_data.append(trace_data.iloc[0]) result = trace_spec.trace_patch.copy() or {} fit_results = None hover_header = "" @@ -173,7 +173,7 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref): if k == "dimensions": dims = [ (name, column) - for (name, column) in g.iteritems() + for (name, column) in trace_data.iteritems() if ((not v) or (name in v)) and ( trace_spec.constructor != go.Parcoords @@ -207,7 +207,7 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref): if k == "size": if "marker" not in result: result["marker"] = dict() - result["marker"]["size"] = g[v] + result["marker"]["size"] = trace_data[v] result["marker"]["sizemode"] = "area" result["marker"]["sizeref"] = sizeref mapping_labels[v_label] = "%{marker.size}" @@ -218,13 +218,18 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref): 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: + if ( + v 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"]] + sorted_trace_data = trace_data.sort_values(by=args["x"]) + y = sorted_trace_data[args["y"]] + x = sorted_trace_data[args["x"]] result["x"] = x if x.dtype.type == np.datetime64: @@ -255,9 +260,9 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref): arr = "arrayminus" if k.endswith("minus") else "array" if error_xy not in result: result[error_xy] = {} - result[error_xy][arr] = g[v] + result[error_xy][arr] = trace_data[v] elif k == "custom_data": - result["customdata"] = g[v].values + result["customdata"] = trace_data[v].values custom_data_len = len(v) # number of custom data columns elif k == "hover_name": if trace_spec.constructor not in [ @@ -265,7 +270,7 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref): go.Histogram2d, go.Histogram2dContour, ]: - result["hovertext"] = g[v] + result["hovertext"] = trace_data[v] if hover_header == "": hover_header = "%{hovertext}

" elif k == "hover_data": @@ -282,15 +287,18 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref): custom_data_len += 1 if "customdata" in result: result["customdata"] = np.hstack( - (result["customdata"], g[col].values[:, None]) + ( + result["customdata"], + trace_data[col].values[:, None], + ) ) else: - result["customdata"] = g[col].values[:, None] + result["customdata"] = trace_data[col].values[:, None] v_label_col = get_decorated_label(args, col, None) mapping_labels[v_label_col] = "%%{customdata[%d]}" % (position) elif k == "color": if trace_spec.constructor in [go.Choropleth, go.Choroplethmapbox]: - result["z"] = g[v] + result["z"] = trace_data[v] result["coloraxis"] = "coloraxis1" mapping_labels[v_label] = "%{z}" elif trace_spec.constructor in [ @@ -303,13 +311,13 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref): result["marker"] = dict() if args.get("color_is_continuous"): - result["marker"]["colors"] = g[v] + result["marker"]["colors"] = trace_data[v] result["marker"]["coloraxis"] = "coloraxis1" mapping_labels[v_label] = "%{color}" else: result["marker"]["colors"] = [] mapping = {} - for cat in g[v]: + for cat in trace_data[v]: if mapping.get(cat) is None: mapping[cat] = args["color_discrete_sequence"][ len(mapping) % len(args["color_discrete_sequence"]) @@ -321,24 +329,24 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref): colorable = "line" if colorable not in result: result[colorable] = dict() - result[colorable]["color"] = g[v] + result[colorable]["color"] = trace_data[v] result[colorable]["coloraxis"] = "coloraxis1" mapping_labels[v_label] = "%%{%s.color}" % colorable elif k == "animation_group": - result["ids"] = g[v] + result["ids"] = trace_data[v] elif k == "locations": - result[k] = g[v] + result[k] = trace_data[v] mapping_labels[v_label] = "%{location}" elif k == "values": - result[k] = g[v] + result[k] = trace_data[v] _label = "value" if v_label == "values" else v_label mapping_labels[_label] = "%{value}" elif k == "parents": - result[k] = g[v] + result[k] = trace_data[v] _label = "parent" if v_label == "parents" else v_label mapping_labels[_label] = "%{parent}" elif k == "ids": - result[k] = g[v] + result[k] = trace_data[v] _label = "id" if v_label == "ids" else v_label mapping_labels[_label] = "%{id}" elif k == "names": @@ -348,14 +356,14 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref): go.Pie, go.Funnelarea, ]: - result["labels"] = g[v] + result["labels"] = trace_data[v] _label = "label" if v_label == "names" else v_label mapping_labels[_label] = "%{label}" else: - result[k] = g[v] + result[k] = trace_data[v] else: if v: - result[k] = g[v] + result[k] = trace_data[v] mapping_labels[v_label] = "%%{%s}" % k if trace_spec.constructor not in [ go.Parcoords, From ca8735394ae79c21b88c1fca2989a15af5d4bbad Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Tue, 28 Jan 2020 15:26:01 -0500 Subject: [PATCH 3/6] k -> attr_name --- .../python/plotly/plotly/express/_core.py | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index 8e8d2b2ecb6..0340002908f 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -167,10 +167,10 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): 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: + v = args[attr_name] + v_label = get_decorated_label(args, v, attr_name) + if attr_name == "dimensions": dims = [ (name, column) for (name, column) in trace_data.iteritems() @@ -198,26 +198,26 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): elif ( v is not None - or (trace_spec.constructor == go.Histogram and k in ["x", "y"]) + 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 attr_name == "size": if "marker" not in result: result["marker"] = dict() result["marker"]["size"] = trace_data[v] result["marker"]["sizemode"] = "area" result["marker"]["sizeref"] = sizeref mapping_labels[v_label] = "%{marker.size}" - elif k == "marginal_x": + 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": + elif attr_name == "trendline": if ( v in ["ols", "lowess"] and args["x"] @@ -255,16 +255,16 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): mapping_labels[get_label(args, args["x"])] = "%{x}" mapping_labels[get_label(args, args["y"])] = "%{y} (trend)" - elif k.startswith("error"): - error_xy = k[:7] - arr = "arrayminus" if k.endswith("minus") else "array" + elif attr_name.startswith("error"): + error_xy = attr_name[:7] + arr = "arrayminus" if attr_name.endswith("minus") else "array" if error_xy not in result: result[error_xy] = {} result[error_xy][arr] = trace_data[v] - elif k == "custom_data": + elif attr_name == "custom_data": result["customdata"] = trace_data[v].values custom_data_len = len(v) # number of custom data columns - elif k == "hover_name": + elif attr_name == "hover_name": if trace_spec.constructor not in [ go.Histogram, go.Histogram2d, @@ -273,7 +273,7 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): result["hovertext"] = trace_data[v] if hover_header == "": hover_header = "%{hovertext}

" - elif k == "hover_data": + elif attr_name == "hover_data": if trace_spec.constructor not in [ go.Histogram, go.Histogram2d, @@ -296,7 +296,7 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): result["customdata"] = trace_data[col].values[:, None] v_label_col = get_decorated_label(args, col, None) mapping_labels[v_label_col] = "%%{customdata[%d]}" % (position) - elif k == "color": + elif attr_name == "color": if trace_spec.constructor in [go.Choropleth, go.Choroplethmapbox]: result["z"] = trace_data[v] result["coloraxis"] = "coloraxis1" @@ -332,24 +332,24 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): result[colorable]["color"] = trace_data[v] result[colorable]["coloraxis"] = "coloraxis1" mapping_labels[v_label] = "%%{%s.color}" % colorable - elif k == "animation_group": + elif attr_name == "animation_group": result["ids"] = trace_data[v] - elif k == "locations": - result[k] = trace_data[v] + elif attr_name == "locations": + result[attr_name] = trace_data[v] mapping_labels[v_label] = "%{location}" - elif k == "values": - result[k] = trace_data[v] + elif attr_name == "values": + result[attr_name] = trace_data[v] _label = "value" if v_label == "values" else v_label mapping_labels[_label] = "%{value}" - elif k == "parents": - result[k] = trace_data[v] + elif attr_name == "parents": + result[attr_name] = trace_data[v] _label = "parent" if v_label == "parents" else v_label mapping_labels[_label] = "%{parent}" - elif k == "ids": - result[k] = trace_data[v] + elif attr_name == "ids": + result[attr_name] = trace_data[v] _label = "id" if v_label == "ids" else v_label mapping_labels[_label] = "%{id}" - elif k == "names": + elif attr_name == "names": if trace_spec.constructor in [ go.Sunburst, go.Treemap, @@ -360,11 +360,11 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): _label = "label" if v_label == "names" else v_label mapping_labels[_label] = "%{label}" else: - result[k] = trace_data[v] + result[attr_name] = trace_data[v] else: if v: - result[k] = trace_data[v] - mapping_labels[v_label] = "%%{%s}" % k + result[attr_name] = trace_data[v] + mapping_labels[v_label] = "%%{%s}" % attr_name if trace_spec.constructor not in [ go.Parcoords, go.Parcats, From e7ced12d10be20c7d61516798722dbb23d43cb41 Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Tue, 28 Jan 2020 15:27:53 -0500 Subject: [PATCH 4/6] v -> attr_value --- .../python/plotly/plotly/express/_core.py | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index 0340002908f..1e0b2690878 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -168,20 +168,20 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): hover_header = "" custom_data_len = 0 for attr_name in trace_spec.attrs: - v = args[attr_name] - v_label = get_decorated_label(args, v, attr_name) + attr_value = args[attr_name] + v_label = get_decorated_label(args, attr_value, attr_name) if attr_name == "dimensions": dims = [ (name, column) for (name, column) in trace_data.iteritems() - if ((not v) or (name in v)) + 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"] ) @@ -197,7 +197,7 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): mapping_labels["%{yaxis.title.text}"] = "%{y}" elif ( - v is not None + 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] @@ -207,7 +207,7 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): if attr_name == "size": if "marker" not in result: result["marker"] = dict() - result["marker"]["size"] = trace_data[v] + result["marker"]["size"] = trace_data[attr_value] result["marker"]["sizemode"] = "area" result["marker"]["sizeref"] = sizeref mapping_labels[v_label] = "%{marker.size}" @@ -219,7 +219,7 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): mapping_labels["count"] = "%{x}" elif attr_name == "trendline": if ( - v in ["ols", "lowess"] + attr_value in ["ols", "lowess"] and args["x"] and args["y"] and len(trace_data) > 1 @@ -235,11 +235,11 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): 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] hover_header = "LOWESS trendline

" - elif v == "ols": + elif attr_value == "ols": fit_results = sm.OLS(y.values, sm.add_constant(x.values)).fit() result["y"] = fit_results.predict() hover_header = "OLS trendline
" @@ -260,17 +260,17 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): arr = "arrayminus" if attr_name.endswith("minus") else "array" if error_xy not in result: result[error_xy] = {} - result[error_xy][arr] = trace_data[v] + result[error_xy][arr] = trace_data[attr_value] elif attr_name == "custom_data": - result["customdata"] = trace_data[v].values - custom_data_len = len(v) # number of custom data columns + result["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"] = trace_data[v] + result["hovertext"] = trace_data[attr_value] if hover_header == "": hover_header = "%{hovertext}

" elif attr_name == "hover_data": @@ -279,7 +279,7 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): go.Histogram2d, go.Histogram2dContour, ]: - for col in v: + for col in attr_value: try: position = args["custom_data"].index(col) except (ValueError, AttributeError, KeyError): @@ -298,7 +298,7 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): mapping_labels[v_label_col] = "%%{customdata[%d]}" % (position) elif attr_name == "color": if trace_spec.constructor in [go.Choropleth, go.Choroplethmapbox]: - result["z"] = trace_data[v] + result["z"] = trace_data[attr_value] result["coloraxis"] = "coloraxis1" mapping_labels[v_label] = "%{z}" elif trace_spec.constructor in [ @@ -311,13 +311,13 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): result["marker"] = dict() if args.get("color_is_continuous"): - result["marker"]["colors"] = trace_data[v] + result["marker"]["colors"] = trace_data[attr_value] result["marker"]["coloraxis"] = "coloraxis1" mapping_labels[v_label] = "%{color}" else: result["marker"]["colors"] = [] mapping = {} - for cat in trace_data[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"]) @@ -329,24 +329,24 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): colorable = "line" if colorable not in result: result[colorable] = dict() - result[colorable]["color"] = trace_data[v] + result[colorable]["color"] = trace_data[attr_value] result[colorable]["coloraxis"] = "coloraxis1" mapping_labels[v_label] = "%%{%s.color}" % colorable elif attr_name == "animation_group": - result["ids"] = trace_data[v] + result["ids"] = trace_data[attr_value] elif attr_name == "locations": - result[attr_name] = trace_data[v] + result[attr_name] = trace_data[attr_value] mapping_labels[v_label] = "%{location}" elif attr_name == "values": - result[attr_name] = trace_data[v] + result[attr_name] = trace_data[attr_value] _label = "value" if v_label == "values" else v_label mapping_labels[_label] = "%{value}" elif attr_name == "parents": - result[attr_name] = trace_data[v] + result[attr_name] = trace_data[attr_value] _label = "parent" if v_label == "parents" else v_label mapping_labels[_label] = "%{parent}" elif attr_name == "ids": - result[attr_name] = trace_data[v] + result[attr_name] = trace_data[attr_value] _label = "id" if v_label == "ids" else v_label mapping_labels[_label] = "%{id}" elif attr_name == "names": @@ -356,14 +356,14 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): go.Pie, go.Funnelarea, ]: - result["labels"] = trace_data[v] + result["labels"] = trace_data[attr_value] _label = "label" if v_label == "names" else v_label mapping_labels[_label] = "%{label}" else: - result[attr_name] = trace_data[v] + result[attr_name] = trace_data[attr_value] else: - if v: - result[attr_name] = trace_data[v] + if attr_value: + result[attr_name] = trace_data[attr_value] mapping_labels[v_label] = "%%{%s}" % attr_name if trace_spec.constructor not in [ go.Parcoords, From 94303bcab58fa14cfa3298cc44cbd5ef4d3089b8 Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Tue, 28 Jan 2020 15:28:43 -0500 Subject: [PATCH 5/6] v_label -> attr_label --- .../python/plotly/plotly/express/_core.py | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index 1e0b2690878..ec332e6d6bd 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -169,7 +169,7 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): custom_data_len = 0 for attr_name in trace_spec.attrs: attr_value = args[attr_name] - v_label = get_decorated_label(args, attr_value, attr_name) + attr_label = get_decorated_label(args, attr_value, attr_name) if attr_name == "dimensions": dims = [ (name, column) @@ -210,7 +210,7 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): result["marker"]["size"] = trace_data[attr_value] result["marker"]["sizemode"] = "area" result["marker"]["sizeref"] = sizeref - mapping_labels[v_label] = "%{marker.size}" + mapping_labels[attr_label] = "%{marker.size}" elif attr_name == "marginal_x": if trace_spec.constructor == go.Histogram: mapping_labels["count"] = "%{y}" @@ -294,13 +294,15 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): ) else: result["customdata"] = trace_data[col].values[:, None] - v_label_col = get_decorated_label(args, col, None) - mapping_labels[v_label_col] = "%%{customdata[%d]}" % (position) + 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"] = trace_data[attr_value] result["coloraxis"] = "coloraxis1" - mapping_labels[v_label] = "%{z}" + mapping_labels[attr_label] = "%{z}" elif trace_spec.constructor in [ go.Sunburst, go.Treemap, @@ -313,7 +315,7 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): if args.get("color_is_continuous"): result["marker"]["colors"] = trace_data[attr_value] result["marker"]["coloraxis"] = "coloraxis1" - mapping_labels[v_label] = "%{color}" + mapping_labels[attr_label] = "%{color}" else: result["marker"]["colors"] = [] mapping = {} @@ -331,23 +333,23 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): result[colorable] = dict() result[colorable]["color"] = trace_data[attr_value] result[colorable]["coloraxis"] = "coloraxis1" - mapping_labels[v_label] = "%%{%s.color}" % colorable + mapping_labels[attr_label] = "%%{%s.color}" % colorable elif attr_name == "animation_group": result["ids"] = trace_data[attr_value] elif attr_name == "locations": result[attr_name] = trace_data[attr_value] - mapping_labels[v_label] = "%{location}" + mapping_labels[attr_label] = "%{location}" elif attr_name == "values": result[attr_name] = trace_data[attr_value] - _label = "value" if v_label == "values" else v_label + _label = "value" if attr_label == "values" else attr_label mapping_labels[_label] = "%{value}" elif attr_name == "parents": result[attr_name] = trace_data[attr_value] - _label = "parent" if v_label == "parents" else v_label + _label = "parent" if attr_label == "parents" else attr_label mapping_labels[_label] = "%{parent}" elif attr_name == "ids": result[attr_name] = trace_data[attr_value] - _label = "id" if v_label == "ids" else v_label + _label = "id" if attr_label == "ids" else attr_label mapping_labels[_label] = "%{id}" elif attr_name == "names": if trace_spec.constructor in [ @@ -357,14 +359,14 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): go.Funnelarea, ]: result["labels"] = trace_data[attr_value] - _label = "label" if v_label == "names" else v_label + _label = "label" if attr_label == "names" else attr_label mapping_labels[_label] = "%{label}" else: result[attr_name] = trace_data[attr_value] else: if attr_value: result[attr_name] = trace_data[attr_value] - mapping_labels[v_label] = "%%{%s}" % attr_name + mapping_labels[attr_label] = "%%{%s}" % attr_name if trace_spec.constructor not in [ go.Parcoords, go.Parcats, From 95f78c2053cd1594e9dc7569558cf7addd3b2467 Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Tue, 28 Jan 2020 15:30:56 -0500 Subject: [PATCH 6/6] result -> trace_patch --- .../python/plotly/plotly/express/_core.py | 88 ++++++++++--------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index ec332e6d6bd..dd670c26d40 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -156,14 +156,14 @@ def make_trace_kwargs(args, trace_spec, trace_data, 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"]: trace_data = trace_data.append(trace_data.iloc[0]) - result = trace_spec.trace_patch.copy() or {} + trace_patch = trace_spec.trace_patch.copy() or {} fit_results = None hover_header = "" custom_data_len = 0 @@ -186,12 +186,12 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): <= 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}" @@ -205,11 +205,11 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): ) ): if attr_name == "size": - if "marker" not in result: - result["marker"] = dict() - result["marker"]["size"] = trace_data[attr_value] - result["marker"]["sizemode"] = "area" - result["marker"]["sizeref"] = sizeref + 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: @@ -230,18 +230,18 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): sorted_trace_data = trace_data.sort_values(by=args["x"]) y = sorted_trace_data[args["y"]] x = sorted_trace_data[args["x"]] - result["x"] = x + trace_patch["x"] = x if x.dtype.type == np.datetime64: x = x.astype(int) / 10 ** 9 # convert to unix epoch seconds if attr_value == "lowess": trendline = sm.nonparametric.lowess(y, x) - result["y"] = trendline[:, 1] + trace_patch["y"] = trendline[:, 1] hover_header = "LOWESS trendline

" 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 = "OLS trendline
" hover_header += "%s = %g * %s + %g
" % ( args["y"], @@ -258,11 +258,11 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): elif attr_name.startswith("error"): error_xy = attr_name[:7] arr = "arrayminus" if attr_name.endswith("minus") else "array" - if error_xy not in result: - result[error_xy] = {} - result[error_xy][arr] = trace_data[attr_value] + 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": - result["customdata"] = trace_data[attr_value].values + 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 [ @@ -270,7 +270,7 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): go.Histogram2d, go.Histogram2dContour, ]: - result["hovertext"] = trace_data[attr_value] + trace_patch["hovertext"] = trace_data[attr_value] if hover_header == "": hover_header = "%{hovertext}

" elif attr_name == "hover_data": @@ -285,23 +285,25 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): except (ValueError, AttributeError, KeyError): position = custom_data_len custom_data_len += 1 - if "customdata" in result: - result["customdata"] = np.hstack( + if "customdata" in trace_patch: + trace_patch["customdata"] = np.hstack( ( - result["customdata"], + trace_patch["customdata"], trace_data[col].values[:, None], ) ) else: - result["customdata"] = trace_data[col].values[:, None] + 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"] = trace_data[attr_value] - result["coloraxis"] = "coloraxis1" + trace_patch["z"] = trace_data[attr_value] + trace_patch["coloraxis"] = "coloraxis1" mapping_labels[attr_label] = "%{z}" elif trace_spec.constructor in [ go.Sunburst, @@ -309,46 +311,46 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): 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"] = trace_data[attr_value] - result["marker"]["coloraxis"] = "coloraxis1" + 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 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"] = trace_data[attr_value] - result[colorable]["coloraxis"] = "coloraxis1" + 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": - result["ids"] = trace_data[attr_value] + trace_patch["ids"] = trace_data[attr_value] elif attr_name == "locations": - result[attr_name] = trace_data[attr_value] + trace_patch[attr_name] = trace_data[attr_value] mapping_labels[attr_label] = "%{location}" elif attr_name == "values": - result[attr_name] = trace_data[attr_value] + trace_patch[attr_name] = trace_data[attr_value] _label = "value" if attr_label == "values" else attr_label mapping_labels[_label] = "%{value}" elif attr_name == "parents": - result[attr_name] = trace_data[attr_value] + trace_patch[attr_name] = trace_data[attr_value] _label = "parent" if attr_label == "parents" else attr_label mapping_labels[_label] = "%{parent}" elif attr_name == "ids": - result[attr_name] = trace_data[attr_value] + trace_patch[attr_name] = trace_data[attr_value] _label = "id" if attr_label == "ids" else attr_label mapping_labels[_label] = "%{id}" elif attr_name == "names": @@ -358,22 +360,22 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): go.Pie, go.Funnelarea, ]: - result["labels"] = trace_data[attr_value] + trace_patch["labels"] = trace_data[attr_value] _label = "label" if attr_label == "names" else attr_label mapping_labels[_label] = "%{label}" else: - result[attr_name] = trace_data[attr_value] + trace_patch[attr_name] = trace_data[attr_value] else: if attr_value: - result[attr_name] = trace_data[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 + "
".join(hover_lines) - return result, fit_results + trace_patch["hovertemplate"] = hover_header + "
".join(hover_lines) + return trace_patch, fit_results def configure_axes(args, constructor, fig, orders):