From 34a563569de281a9a2af614d89fc2dcc2dac4703 Mon Sep 17 00:00:00 2001 From: Jyotishman Das <107503099+rishi02102017@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:00:24 +0530 Subject: [PATCH 1/2] Respect facet_row_spacing=0 and facet_col_spacing=0 in Plotly Express init_figure picked the subplot spacing with `args.get("facet_row_spacing") or 0.07` (and similar for the other defaults), so an explicit spacing of 0 was treated as unset and the default gap was used instead. The docstrings allow any float in [0, 1] and make_subplots accepts 0, so fall back to the default only when the argument is None. px.imshow builds its facets through the same code and is fixed too. --- CHANGELOG.md | 1 + plotly/express/_core.py | 16 ++++++----- tests/test_optional/test_px/test_facets.py | 31 ++++++++++++++++++++++ tests/test_optional/test_px/test_imshow.py | 9 +++++++ 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f19cf28a27b..11ecd1d7355 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 984d7c90fc7..3b447f7c021 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 593b214ed1e..9afbb947b92 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) diff --git a/tests/test_optional/test_px/test_imshow.py b/tests/test_optional/test_px/test_imshow.py index 3a0e230eb9c..be984baccef 100644 --- a/tests/test_optional/test_px/test_imshow.py +++ b/tests/test_optional/test_px/test_imshow.py @@ -553,3 +553,12 @@ def test_imshow_xarray_facet_row_and_col(): annotations = [a.text for a in fig.layout.annotations] assert any("row_dim=R1" in a for a in annotations) assert any("col_dim=C1" in a for a in annotations) + + +def test_imshow_facet_spacing_zero(): + img = np.random.random((2, 3, 5, 6)) + fig = px.imshow( + img, facet_row=0, facet_col=1, facet_row_spacing=0, facet_col_spacing=0 + ) + assert fig.layout.xaxis2.domain[0] - fig.layout.xaxis.domain[1] == pytest.approx(0) + assert fig.layout.yaxis4.domain[0] - fig.layout.yaxis.domain[1] == pytest.approx(0) From 5de9b462d22470c5a2e10bab2ec21d50510ca5e2 Mon Sep 17 00:00:00 2001 From: Jyotishman Das <107503099+rishi02102017@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:26:30 +0530 Subject: [PATCH 2/2] Remove duplicate imshow spacing test It checked the same thing as the first part of test_facets_zero_spacing. --- tests/test_optional/test_px/test_imshow.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/test_optional/test_px/test_imshow.py b/tests/test_optional/test_px/test_imshow.py index be984baccef..3a0e230eb9c 100644 --- a/tests/test_optional/test_px/test_imshow.py +++ b/tests/test_optional/test_px/test_imshow.py @@ -553,12 +553,3 @@ def test_imshow_xarray_facet_row_and_col(): annotations = [a.text for a in fig.layout.annotations] assert any("row_dim=R1" in a for a in annotations) assert any("col_dim=C1" in a for a in annotations) - - -def test_imshow_facet_spacing_zero(): - img = np.random.random((2, 3, 5, 6)) - fig = px.imshow( - img, facet_row=0, facet_col=1, facet_row_spacing=0, facet_col_spacing=0 - ) - assert fig.layout.xaxis2.domain[0] - fig.layout.xaxis.domain[1] == pytest.approx(0) - assert fig.layout.yaxis4.domain[0] - fig.layout.yaxis.domain[1] == pytest.approx(0)