Skip to content

Commit 0206a05

Browse files
authored
Add show parameter to .pl.show() to control plt.show() behavior (#537)
1 parent f2b9996 commit 0206a05

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/spatialdata_plot/pl/basic.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pathlib import Path
88
from typing import Any, Literal, cast
99

10+
import matplotlib
1011
import matplotlib.pyplot as plt
1112
import numpy as np
1213
import pandas as pd
@@ -789,6 +790,7 @@ def show(
789790
ax: list[Axes] | Axes | None = None,
790791
return_ax: bool = False,
791792
save: str | Path | None = None,
793+
show: bool | None = None,
792794
) -> sd.SpatialData:
793795
"""
794796
Plot the images in the SpatialData object.
@@ -813,6 +815,12 @@ def show(
813815
Number of columns in the figure. Default is 4.
814816
return_ax :
815817
Whether to return the axes object created. False by default.
818+
show :
819+
Whether to call ``plt.show()`` at the end. If ``None`` (default), the plot is shown
820+
automatically when running in non-interactive mode (scripts) and suppressed in
821+
interactive sessions (e.g. Jupyter). Set to ``False`` to prevent ``plt.show()``
822+
from being called, which is useful when you want to save or further modify the
823+
figure after calling this method.
816824
colorbar :
817825
Global switch to enable/disable all colorbars. Per-layer settings are ignored when this is False.
818826
colorbar_params :
@@ -857,6 +865,7 @@ def show(
857865
ax,
858866
return_ax,
859867
save,
868+
show,
860869
)
861870

862871
sdata = self._copy()
@@ -1212,8 +1221,12 @@ def _draw_colorbar(
12121221
if fig_params.fig is not None and save is not None:
12131222
save_fig(fig_params.fig, path=save)
12141223

1215-
# Manually show plot if we're not in interactive mode
1216-
# https://stackoverflow.com/a/64523765
1217-
if not hasattr(sys, "ps1"):
1224+
# Show the plot unless the caller opted out.
1225+
# Default (show=None): display in non-interactive mode (scripts), suppress in interactive
1226+
# sessions. We check both sys.ps1 (standard REPL) and matplotlib.is_interactive()
1227+
# (covers IPython, Jupyter, plt.ion(), and IDE consoles like PyCharm).
1228+
if show is None:
1229+
show = not hasattr(sys, "ps1") and not matplotlib.is_interactive()
1230+
if show:
12181231
plt.show()
12191232
return (fig_params.ax if fig_params.axs is None else fig_params.axs) if return_ax else None # shuts up ruff

src/spatialdata_plot/pl/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,6 +2000,7 @@ def _validate_show_parameters(
20002000
ax: list[Axes] | Axes | None,
20012001
return_ax: bool,
20022002
save: str | Path | None,
2003+
show: bool | None,
20032004
) -> None:
20042005
if coordinate_systems is not None and not isinstance(coordinate_systems, list | str):
20052006
raise TypeError("Parameter 'coordinate_systems' must be a string or a list of strings.")
@@ -2089,6 +2090,9 @@ def _validate_show_parameters(
20892090
if save is not None and not isinstance(save, str | Path):
20902091
raise TypeError("Parameter 'save' must be a string or a pathlib.Path.")
20912092

2093+
if show is not None and not isinstance(show, bool):
2094+
raise TypeError("Parameter 'show' must be a boolean or None.")
2095+
20922096

20932097
def _type_check_params(param_dict: dict[str, Any], element_type: str) -> dict[str, Any]:
20942098
colorbar = param_dict.get("colorbar", "auto")

0 commit comments

Comments
 (0)