Skip to content

Commit bcc6863

Browse files
authored
Merge pull request #5780 from jayzhou2309/fix/overall-trendline-marginals
Keep overall trendline off marginal subplots
2 parents 84498cc + 46a9e5c commit bcc6863

3 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2121
- Fix `convert_dash` mapping dotted lines (`linestyle=':'`) to invalid `'circle'` in Plotly, correctly converting them to `'dot'` and preserving custom dash patterns as pixel lists, [[#5719](https://github.com/plotly/plotly.py/pull/5719)] with thanks to @robertoffmoura for the contribution!
2222
- Fix Plotly Express mutating lists passed to the `x` or `y` arguments in wide mode [[#5727](https://github.com/plotly/plotly.py/pull/5727)], with thanks to @cpruijsen for the contribution!
2323
- Fix `px.sunburst`, `px.treemap` and `px.icicle` listing sectors in a different order on every run when `path` is used with a Polars DataFrame; sectors now follow their order of first appearance for all dataframe backends [[#5766](https://github.com/plotly/plotly.py/pull/5766)], with thanks to @Irahan2 for the contribution!
24+
- Fix `trendline_scope="overall"` drawing the overall trendline on the `marginal_x`/`marginal_y` subplots as well as the main plot; it is now drawn only on the main subplots [[#5780](https://github.com/plotly/plotly.py/pull/5780)], with thanks to @jayzhou2309 for the contribution!
2425
- Fix `mpl_to_plotly` conversion of matplotlib contour lines and line collections: close contour rings ending with `Z` codes, support dash styles, convert date x-axes, separate disjoint subpaths with `None` separators, hide line collection traces from the legend by default, and group consecutive same-style lines into single traces [[#5770](https://github.com/plotly/plotly.py/pull/5770)], with thanks to @robertoffmoura for the contribution!
2526
- Fix `plotly.io.write_images` ignoring `layout.width`/`layout.height` (and the template's) and exporting every figure at the default 700x500 size; it now uses the same width/height priority as `write_image` and `to_image` [[#5784](https://github.com/plotly/plotly.py/pull/5784)], with thanks to @breken-ai for the contribution!
2627
- Fix color utilities mutating the list of colors passed to them [[#5836](https://github.com/plotly/plotly.py/pull/5836)], with thanks to @Enesuygurs for the contribution!

‎plotly/express/_core.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,9 +2834,13 @@ def make_figure(args, constructor, trace_patch=None, layout_patch=None):
28342834
args, trendline_spec, args["data_frame"], {}, sizeref
28352835
)
28362836
trendline_trace.update(patch)
2837-
fig.add_trace(
2838-
trendline_trace, row="all", col="all", exclude_empty_subplots=True
2839-
)
2837+
main_rows = nrows - 1 if args.get("marginal_x") is not None else nrows
2838+
main_cols = ncols - 1 if args.get("marginal_y") is not None else ncols
2839+
for row in range(1, main_rows + 1):
2840+
for col in range(1, main_cols + 1):
2841+
fig.add_trace(
2842+
trendline_trace, row=row, col=col, exclude_empty_subplots=True
2843+
)
28402844
fig.update_traces(selector=-1, showlegend=True)
28412845
if fit_results is not None:
28422846
trendline_rows.append(dict(px_fit_results=fit_results))

‎tests/test_optional/test_px/test_trendline.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,34 @@ def test_overall_trendline(backend):
264264
params3 = results3["px_fit_results"].iloc[0].params
265265

266266
assert np.all(np.array_equal(params1, params3))
267+
268+
269+
@pytest.mark.parametrize(
270+
"kwargs",
271+
[
272+
dict(marginal_x="box", marginal_y="box"),
273+
dict(marginal_x="histogram", facet_col="smoker"),
274+
dict(marginal_y="violin", facet_row="smoker"),
275+
],
276+
)
277+
def test_overall_trendline_skips_marginals(backend, kwargs):
278+
df = px.data.tips(return_type=backend)
279+
fig = px.scatter(
280+
df,
281+
x="total_bill",
282+
y="tip",
283+
color="sex",
284+
trendline="ols",
285+
trendline_scope="overall",
286+
**kwargs,
287+
)
288+
trendline_axes = [
289+
(t.xaxis, t.yaxis) for t in fig.data if t.name == "Overall Trendline"
290+
]
291+
scatter_axes = {
292+
(t.xaxis, t.yaxis)
293+
for t in fig.data
294+
if t.type == "scatter" and t.name != "Overall Trendline"
295+
}
296+
assert len(trendline_axes) == len(scatter_axes)
297+
assert set(trendline_axes) == scatter_axes

0 commit comments

Comments
 (0)