5454)
5555from spatialdata_plot .pl .utils import (
5656 _RENDER_CMD_TO_CS_FLAG ,
57+ _draw_scalebar ,
5758 _get_cs_contents ,
5859 _get_elements_to_be_rendered ,
5960 _get_valid_cs ,
@@ -890,6 +891,10 @@ def show(
890891 return_ax : bool = False ,
891892 save : str | Path | None = None ,
892893 show : bool | None = None ,
894+ scalebar_dx : float | None = None ,
895+ scalebar_units : str = "um" ,
896+ scalebar_params : dict [str , Any ] | None = None ,
897+ legend_params : dict [str , Any ] | None = None ,
893898 ) -> Axes | list [Axes ] | None :
894899 """
895900 Execute the plotting tree and display the final figure.
@@ -950,6 +955,22 @@ def show(
950955 automatically when running in non-interactive mode (scripts) and suppressed in
951956 interactive sessions (e.g. Jupyter). When ``ax`` is provided by the user, defaults
952957 to ``False`` to allow further modifications.
958+ scalebar_dx : float | None
959+ Physical size of one axes-unit in ``scalebar_units``. If ``None``, no scalebar is drawn.
960+ SpatialData coordinate systems carry no unit metadata, so this value must be supplied
961+ explicitly (e.g. ``1.0`` when axes are already in micrometers; the microns-per-pixel
962+ value when axes are in image pixels).
963+ scalebar_units : str, default "um"
964+ Unit string for the scalebar (passed to :class:`matplotlib_scalebar.scalebar.ScaleBar`).
965+ Only takes effect when ``scalebar_dx`` is set.
966+ scalebar_params : dict[str, Any] | None
967+ Extra keyword arguments forwarded to :class:`matplotlib_scalebar.scalebar.ScaleBar`,
968+ e.g. ``{"location": "lower right", "color": "white", "length_fraction": 0.25}``.
969+ See the matplotlib-scalebar documentation for the full list of options.
970+ legend_params : dict[str, Any] | None
971+ Bundled legend options; overrides the matching ``legend_*`` flat kwargs. Accepted keys:
972+ ``location`` (or ``loc``), ``fontsize``, ``fontweight``, ``fontoutline``,
973+ ``na_in_legend``. Unknown keys raise ``ValueError``.
953974
954975 Returns
955976 -------
@@ -987,6 +1008,10 @@ def show(
9871008 return_ax ,
9881009 save ,
9891010 show ,
1011+ scalebar_dx ,
1012+ scalebar_units ,
1013+ scalebar_params ,
1014+ legend_params ,
9901015 )
9911016
9921017 if fig is not None and not isinstance (ax , Sequence ):
@@ -1100,7 +1125,7 @@ def show(
11001125 raise ValueError (msg )
11011126
11021127 # set up canvas
1103- fig_params , scalebar_params = _prepare_params_plot (
1128+ fig_params , scalebar_params_obj = _prepare_params_plot (
11041129 num_panels = len (coordinate_systems ),
11051130 figsize = figsize ,
11061131 dpi = dpi ,
@@ -1110,15 +1135,25 @@ def show(
11101135 hspace = hspace ,
11111136 ncols = ncols ,
11121137 frameon = frameon ,
1138+ scalebar_dx = scalebar_dx ,
1139+ scalebar_units = scalebar_units ,
1140+ scalebar_kwargs = scalebar_params ,
11131141 )
1114- legend_colorbar = colorbar
1115- legend_params = LegendParams (
1142+ if legend_params :
1143+ legend_fontsize = legend_params .get ("fontsize" , legend_fontsize )
1144+ legend_fontweight = legend_params .get ("fontweight" , legend_fontweight )
1145+ # `loc` is matplotlib.Legend's native key; `location` aligns with colorbar/scalebar.
1146+ legend_loc = legend_params .get ("location" , legend_params .get ("loc" , legend_loc ))
1147+ legend_fontoutline = legend_params .get ("fontoutline" , legend_fontoutline )
1148+ na_in_legend = legend_params .get ("na_in_legend" , na_in_legend )
1149+
1150+ legend_params_obj = LegendParams (
11161151 legend_fontsize = legend_fontsize ,
11171152 legend_fontweight = legend_fontweight ,
11181153 legend_loc = legend_loc ,
11191154 legend_fontoutline = legend_fontoutline ,
11201155 na_in_legend = na_in_legend ,
1121- colorbar = legend_colorbar ,
1156+ colorbar = colorbar ,
11221157 )
11231158
11241159 def _draw_colorbar (
@@ -1210,7 +1245,7 @@ def _draw_colorbar(
12101245 has_shapes = cs_row ["has_shapes" ]
12111246 ax = fig_params .ax if fig_params .axs is None else fig_params .axs [i ]
12121247 assert isinstance (ax , Axes )
1213- axis_colorbar_requests : list [ColorbarSpec ] | None = [] if legend_params .colorbar else None
1248+ axis_colorbar_requests : list [ColorbarSpec ] | None = [] if legend_params_obj .colorbar else None
12141249 axis_channel_legend_entries : list [ChannelLegendEntry ] = []
12151250
12161251 wants_images = False
@@ -1239,8 +1274,7 @@ def _draw_colorbar(
12391274 coordinate_system = cs ,
12401275 ax = ax ,
12411276 fig_params = fig_params ,
1242- scalebar_params = scalebar_params ,
1243- legend_params = legend_params ,
1277+ legend_params = legend_params_obj ,
12441278 colorbar_requests = axis_colorbar_requests ,
12451279 channel_legend_entries = axis_channel_legend_entries ,
12461280 rasterize = rasterize ,
@@ -1258,8 +1292,7 @@ def _draw_colorbar(
12581292 coordinate_system = cs ,
12591293 ax = ax ,
12601294 fig_params = fig_params ,
1261- scalebar_params = scalebar_params ,
1262- legend_params = legend_params ,
1295+ legend_params = legend_params_obj ,
12631296 colorbar_requests = axis_colorbar_requests ,
12641297 )
12651298
@@ -1275,8 +1308,7 @@ def _draw_colorbar(
12751308 coordinate_system = cs ,
12761309 ax = ax ,
12771310 fig_params = fig_params ,
1278- scalebar_params = scalebar_params ,
1279- legend_params = legend_params ,
1311+ legend_params = legend_params_obj ,
12801312 colorbar_requests = axis_colorbar_requests ,
12811313 )
12821314
@@ -1311,8 +1343,7 @@ def _draw_colorbar(
13111343 coordinate_system = cs ,
13121344 ax = ax ,
13131345 fig_params = fig_params ,
1314- scalebar_params = scalebar_params ,
1315- legend_params = legend_params ,
1346+ legend_params = legend_params_obj ,
13161347 colorbar_requests = axis_colorbar_requests ,
13171348 rasterize = rasterize ,
13181349 )
@@ -1352,11 +1383,13 @@ def _draw_colorbar(
13521383 ax .set_xlim (x_min , x_max )
13531384 ax .set_ylim (y_max , y_min ) # (0, 0) is top-left
13541385
1355- if legend_params .colorbar and axis_colorbar_requests :
1386+ if legend_params_obj .colorbar and axis_colorbar_requests :
13561387 pending_colorbars .append ((ax , axis_colorbar_requests ))
13571388
13581389 if axis_channel_legend_entries :
1359- _draw_channel_legend (ax , axis_channel_legend_entries , legend_params , fig_params )
1390+ _draw_channel_legend (ax , axis_channel_legend_entries , legend_params_obj , fig_params )
1391+
1392+ _draw_scalebar (ax , scalebar_params_obj , panel_idx = i )
13601393
13611394 if pending_colorbars and fig_params .fig is not None :
13621395 fig = fig_params .fig
0 commit comments