diff --git a/CHANGELOG.md b/CHANGELOG.md index f19cf28a27..11ecd1d735 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - 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! - 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! - 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! +- Fix Plotly Express (including `px.imshow`) ignoring `facet_row_spacing=0` and `facet_col_spacing=0` and using the default spacing between facets instead [[#5881](https://github.com/plotly/plotly.py/pull/5881)], with thanks to @rishi02102017 for the contribution! ## [7.1.0] - 2026-09-15 diff --git a/plotly/express/_core.py b/plotly/express/_core.py index 984d7c90fc..3b447f7c02 100644 --- a/plotly/express/_core.py +++ b/plotly/express/_core.py @@ -2869,6 +2869,8 @@ def init_figure(args, subplot_type, frame_list, nrows, ncols, col_labels, row_la column_widths = [1.0] * ncols row_heights = [1.0] * nrows facet_col_wrap = args.get("facet_col_wrap", 0) + facet_row_spacing = args.get("facet_row_spacing") + facet_col_spacing = args.get("facet_col_spacing") # Build column_widths/row_heights if subplot_type == "xy": @@ -2881,9 +2883,9 @@ def init_figure(args, subplot_type, frame_list, nrows, ncols, col_labels, row_la row_heights = [main_size] * (nrows - 1) + [1 - main_size] vertical_spacing = 0.01 elif facet_col_wrap: - vertical_spacing = args.get("facet_row_spacing") or 0.07 + vertical_spacing = 0.07 if facet_row_spacing is None else facet_row_spacing else: - vertical_spacing = args.get("facet_row_spacing") or 0.03 + vertical_spacing = 0.03 if facet_row_spacing is None else facet_row_spacing if args.get("marginal_y") is not None: if args["marginal_y"] == "histogram" or ("color" in args and args["color"]): @@ -2894,7 +2896,9 @@ def init_figure(args, subplot_type, frame_list, nrows, ncols, col_labels, row_la column_widths = [main_size] * (ncols - 1) + [1 - main_size] horizontal_spacing = 0.005 else: - horizontal_spacing = args.get("facet_col_spacing") or 0.02 + horizontal_spacing = ( + 0.02 if facet_col_spacing is None else facet_col_spacing + ) else: # Other subplot types: # 'scene', 'geo', 'polar', 'ternary', 'map', 'domain', None @@ -2902,10 +2906,10 @@ def init_figure(args, subplot_type, frame_list, nrows, ncols, col_labels, row_la # We can customize subplot spacing per type once we enable faceting # for all plot types if facet_col_wrap: - vertical_spacing = args.get("facet_row_spacing") or 0.07 + vertical_spacing = 0.07 if facet_row_spacing is None else facet_row_spacing else: - vertical_spacing = args.get("facet_row_spacing") or 0.03 - horizontal_spacing = args.get("facet_col_spacing") or 0.02 + vertical_spacing = 0.03 if facet_row_spacing is None else facet_row_spacing + horizontal_spacing = 0.02 if facet_col_spacing is None else facet_col_spacing if facet_col_wrap: subplot_labels = [None] * nrows * ncols diff --git a/tests/test_optional/test_px/test_facets.py b/tests/test_optional/test_px/test_facets.py index 593b214ed1..9afbb947b9 100644 --- a/tests/test_optional/test_px/test_facets.py +++ b/tests/test_optional/test_px/test_facets.py @@ -46,6 +46,37 @@ def test_facets(backend): assert fig.layout.yaxis4.domain[0] - fig.layout.yaxis.domain[1] == approx(0.08) +def test_facets_zero_spacing(backend): + df = px.data.tips(return_type=backend) + + fig = px.scatter( + df, + x="total_bill", + y="tip", + facet_row="sex", + facet_col="smoker", + facet_col_spacing=0, + facet_row_spacing=0, + ) + assert fig.layout.xaxis4.domain[0] - fig.layout.xaxis.domain[1] == approx(0) + assert fig.layout.yaxis4.domain[0] - fig.layout.yaxis.domain[1] == approx(0) + + fig = px.scatter( + df, + x="total_bill", + y="tip", + facet_col="day", + facet_col_wrap=2, + facet_col_spacing=0, + facet_row_spacing=0, + ) + assert fig.layout.xaxis4.domain[0] - fig.layout.xaxis.domain[1] == approx(0) + assert fig.layout.yaxis4.domain[0] - fig.layout.yaxis.domain[1] == approx(0) + + fig = px.pie(df, values="tip", names="day", facet_col="smoker", facet_col_spacing=0) + assert fig.data[1].domain.x[0] - fig.data[0].domain.x[1] == approx(0) + + def test_facets_with_marginals(backend): df = px.data.tips(return_type=backend)