77from pathlib import Path
88from typing import Any , Literal , cast
99
10+ import matplotlib
1011import matplotlib .pyplot as plt
1112import numpy as np
1213import 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
0 commit comments