Skip to content

Fix user figure DPI silently overridden by pl.show()#575

Merged
timtreis merged 8 commits intomainfrom
fix/issue-310-dpi-override
Apr 2, 2026
Merged

Fix user figure DPI silently overridden by pl.show()#575
timtreis merged 8 commits intomainfrom
fix/issue-310-dpi-override

Conversation

@timtreis
Copy link
Copy Markdown
Member

@timtreis timtreis commented Apr 2, 2026

Fixes #310.

  • _prepare_params_plot(): preserve user figure DPI when ax= is passed without explicit dpi=
  • _rasterize_if_necessary(): remove blanket < 2000px guard that skipped rasterization regardless of DPI
  • updates other images as well because we have 80 dpi in the visual tests and these images trigger a rasterize

timtreis and others added 8 commits April 2, 2026 14:56
When a user created a figure with a custom DPI (e.g. plt.figure(dpi=300))
and passed its axes to .pl.show(ax=ax), the DPI was silently overwritten
with the rcParams default. This happened because _prepare_params_plot()
unconditionally resolved dpi=None to rcParams["figure.dpi"] and then
called fig.set_dpi() on the user's figure.

Now the figure's existing DPI is preserved when the user provides their
own axes without an explicit dpi= argument. Explicit dpi= still overrides.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
A regression that overrides the user's DPI to the default (~100) would
produce a noticeably sharper render that fails baseline comparison.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…ization

The rasterization code skips images under 2000x2000 regardless of DPI,
so varying DPI has no visible effect on the blobs test data. The unit
tests already verify the DPI value is correctly preserved.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The guard `if x_dims < 2000 and y_dims < 2000: do_rasterization = False`
silently ignored the user's DPI intent — a low-DPI figure should still
trigger downsampling when the source image exceeds the target resolution.

Also re-adds the visual test for #310: at dpi=15 the 512x512 blobs image
is now correctly downsampled to ~96x72, producing visibly pixelated output
that would fail baseline comparison if DPI were overridden to the default.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The pixelated render confirms rasterization now respects the user's
dpi=15 setting — the 512x512 image is downsampled to ~96x72.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
10 tests failed because labels/images now correctly rasterize when
the source (512px) exceeds the target resolution (DPI=80 × canvas).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@timtreis timtreis force-pushed the fix/issue-310-dpi-override branch from bbec8e6 to 66b8446 Compare April 2, 2026 13:37
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.36%. Comparing base (9dc65ce) to head (66b8446).

Files with missing lines Patch % Lines
src/spatialdata_plot/pl/utils.py 66.66% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #575      +/-   ##
==========================================
- Coverage   75.42%   75.36%   -0.06%     
==========================================
  Files          10       10              
  Lines        2934     2935       +1     
  Branches      683      684       +1     
==========================================
- Hits         2213     2212       -1     
- Misses        440      441       +1     
- Partials      281      282       +1     
Files with missing lines Coverage Δ
src/spatialdata_plot/pl/utils.py 65.60% <66.66%> (-0.11%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@timtreis timtreis merged commit 303140c into main Apr 2, 2026
8 checks passed
@timtreis timtreis deleted the fix/issue-310-dpi-override branch April 2, 2026 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Different plot if specifying dpi= in plt.figure() vs in pl.show()

2 participants