Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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"]):
Expand All @@ -2894,18 +2896,20 @@ 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
#
# 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
Expand Down
31 changes: 31 additions & 0 deletions tests/test_optional/test_px/test_facets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 9 additions & 0 deletions tests/test_optional/test_px/test_imshow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)