Skip to content

Add show parameter to .pl.show() to control plt.show() behavior#537

Open
timtreis wants to merge 2 commits intomainfrom
fix/issue494-show-parameter
Open

Add show parameter to .pl.show() to control plt.show() behavior#537
timtreis wants to merge 2 commits intomainfrom
fix/issue494-show-parameter

Conversation

@timtreis
Copy link
Member

Summary

  • Adds a show: bool | None = None parameter to .pl.show() so users can prevent plt.show() from being called.
  • When running scripts, plt.show() was called unconditionally (clearing the figure before plt.savefig()). With show=False, users can now save or further modify the figure.
  • Default (None) preserves current auto-detection behavior via sys.ps1.

Closes #494

Before (script)

fig, axs = plt.subplots(ncols=2)
axs[0].plot([1, 2, 3, 4], [1, 2, 3, 4])
sdata.pl.render_images("blobs_image").pl.show(ax=axs[1])
plt.savefig("test.png")  # blank image — plt.show() already cleared it

After

fig, axs = plt.subplots(ncols=2)
axs[0].plot([1, 2, 3, 4], [1, 2, 3, 4])
sdata.pl.render_images("blobs_image").pl.show(ax=axs[1], show=False)
plt.savefig("test.png")  # works as expected

Behavior matrix

show= Interactive (Jupyter) Script (.py)
None (default) no plt.show() plt.show() (same as before)
True plt.show() plt.show()
False no plt.show() no plt.show()

Test plan

  • show=False prevents plt.show(), saved image is not blank
  • show=None preserves current behavior
  • show=True always calls plt.show()
  • Invalid value (e.g. show="yes") raises TypeError
  • Pre-commit hooks pass (prettier, ruff, ruff format, mypy)

🤖 Generated with Claude Code

When running in scripts, `plt.show()` was called unconditionally, clearing
the figure before users could save it with `plt.savefig()`. The new
`show: bool | None = None` parameter gives users explicit control:

- `None` (default): preserves current behavior (auto-detect via `sys.ps1`)
- `True`: always call `plt.show()`
- `False`: never call `plt.show()`

Closes #494

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@timtreis timtreis marked this pull request as ready for review February 27, 2026 21:49
@codecov-commenter
Copy link

codecov-commenter commented Feb 27, 2026

Codecov Report

❌ Patch coverage is 80.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 83.83%. Comparing base (d7dc3f0) to head (abe6be6).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/spatialdata_plot/pl/utils.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #537      +/-   ##
==========================================
+ Coverage   83.74%   83.83%   +0.08%     
==========================================
  Files           8        8              
  Lines        2553     2573      +20     
==========================================
+ Hits         2138     2157      +19     
- Misses        415      416       +1     
Files with missing lines Coverage Δ
src/spatialdata_plot/pl/basic.py 91.66% <100.00%> (+0.11%) ⬆️
src/spatialdata_plot/pl/utils.py 79.30% <50.00%> (-0.04%) ⬇️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LucaMarconato
Copy link
Member

I wonder if it closes #68

Also check matplotlib.is_interactive() when deciding whether to call
plt.show() automatically. This covers IDE consoles (PyCharm, VS Code)
and plt.ion() sessions that don't set sys.ps1.

Closes #68

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@timtreis
Copy link
Member Author

I added another check for matplotlib.is_interactive() which allegedly covers PyCharm, but I don't use PyCharm, could you verify?

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.

.pl.show() executes plt.show() even when not wanted

3 participants