Skip to content

Commit 36814cf

Browse files
committed
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.
1 parent 3c1aeba commit 36814cf

4 files changed

Lines changed: 51 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2626
- 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!
2727
- 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!
2828
- 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!
29+
- Fix Plotly Express (including `px.imshow`) ignoring `facet_row_spacing=0` and `facet_col_spacing=0` and using the default spacing between facets instead [[#XXXX](https://github.com/plotly/plotly.py/pull/XXXX)], with thanks to @rishi02102017 for the contribution!
2930

3031

3132
## [7.1.0] - 2026-09-15

‎plotly/express/_core.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2869,6 +2869,8 @@ def init_figure(args, subplot_type, frame_list, nrows, ncols, col_labels, row_la
28692869
column_widths = [1.0] * ncols
28702870
row_heights = [1.0] * nrows
28712871
facet_col_wrap = args.get("facet_col_wrap", 0)
2872+
facet_row_spacing = args.get("facet_row_spacing")
2873+
facet_col_spacing = args.get("facet_col_spacing")
28722874

28732875
# Build column_widths/row_heights
28742876
if subplot_type == "xy":
@@ -2881,9 +2883,9 @@ def init_figure(args, subplot_type, frame_list, nrows, ncols, col_labels, row_la
28812883
row_heights = [main_size] * (nrows - 1) + [1 - main_size]
28822884
vertical_spacing = 0.01
28832885
elif facet_col_wrap:
2884-
vertical_spacing = args.get("facet_row_spacing") or 0.07
2886+
vertical_spacing = 0.07 if facet_row_spacing is None else facet_row_spacing
28852887
else:
2886-
vertical_spacing = args.get("facet_row_spacing") or 0.03
2888+
vertical_spacing = 0.03 if facet_row_spacing is None else facet_row_spacing
28872889

28882890
if args.get("marginal_y") is not None:
28892891
if args["marginal_y"] == "histogram" or ("color" in args and args["color"]):
@@ -2894,18 +2896,20 @@ def init_figure(args, subplot_type, frame_list, nrows, ncols, col_labels, row_la
28942896
column_widths = [main_size] * (ncols - 1) + [1 - main_size]
28952897
horizontal_spacing = 0.005
28962898
else:
2897-
horizontal_spacing = args.get("facet_col_spacing") or 0.02
2899+
horizontal_spacing = (
2900+
0.02 if facet_col_spacing is None else facet_col_spacing
2901+
)
28982902
else:
28992903
# Other subplot types:
29002904
# 'scene', 'geo', 'polar', 'ternary', 'map', 'domain', None
29012905
#
29022906
# We can customize subplot spacing per type once we enable faceting
29032907
# for all plot types
29042908
if facet_col_wrap:
2905-
vertical_spacing = args.get("facet_row_spacing") or 0.07
2909+
vertical_spacing = 0.07 if facet_row_spacing is None else facet_row_spacing
29062910
else:
2907-
vertical_spacing = args.get("facet_row_spacing") or 0.03
2908-
horizontal_spacing = args.get("facet_col_spacing") or 0.02
2911+
vertical_spacing = 0.03 if facet_row_spacing is None else facet_row_spacing
2912+
horizontal_spacing = 0.02 if facet_col_spacing is None else facet_col_spacing
29092913

29102914
if facet_col_wrap:
29112915
subplot_labels = [None] * nrows * ncols

‎tests/test_optional/test_px/test_facets.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,37 @@ def test_facets(backend):
4646
assert fig.layout.yaxis4.domain[0] - fig.layout.yaxis.domain[1] == approx(0.08)
4747

4848

49+
def test_facets_zero_spacing(backend):
50+
df = px.data.tips(return_type=backend)
51+
52+
fig = px.scatter(
53+
df,
54+
x="total_bill",
55+
y="tip",
56+
facet_row="sex",
57+
facet_col="smoker",
58+
facet_col_spacing=0,
59+
facet_row_spacing=0,
60+
)
61+
assert fig.layout.xaxis4.domain[0] - fig.layout.xaxis.domain[1] == approx(0)
62+
assert fig.layout.yaxis4.domain[0] - fig.layout.yaxis.domain[1] == approx(0)
63+
64+
fig = px.scatter(
65+
df,
66+
x="total_bill",
67+
y="tip",
68+
facet_col="day",
69+
facet_col_wrap=2,
70+
facet_col_spacing=0,
71+
facet_row_spacing=0,
72+
)
73+
assert fig.layout.xaxis4.domain[0] - fig.layout.xaxis.domain[1] == approx(0)
74+
assert fig.layout.yaxis4.domain[0] - fig.layout.yaxis.domain[1] == approx(0)
75+
76+
fig = px.pie(df, values="tip", names="day", facet_col="smoker", facet_col_spacing=0)
77+
assert fig.data[1].domain.x[0] - fig.data[0].domain.x[1] == approx(0)
78+
79+
4980
def test_facets_with_marginals(backend):
5081
df = px.data.tips(return_type=backend)
5182

‎tests/test_optional/test_px/test_imshow.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,3 +553,12 @@ def test_imshow_xarray_facet_row_and_col():
553553
annotations = [a.text for a in fig.layout.annotations]
554554
assert any("row_dim=R1" in a for a in annotations)
555555
assert any("col_dim=C1" in a for a in annotations)
556+
557+
558+
def test_imshow_facet_spacing_zero():
559+
img = np.random.random((2, 3, 5, 6))
560+
fig = px.imshow(
561+
img, facet_row=0, facet_col=1, facet_row_spacing=0, facet_col_spacing=0
562+
)
563+
assert fig.layout.xaxis2.domain[0] - fig.layout.xaxis.domain[1] == pytest.approx(0)
564+
assert fig.layout.yaxis4.domain[0] - fig.layout.yaxis.domain[1] == pytest.approx(0)

0 commit comments

Comments
 (0)