5151 _FontWeight ,
5252)
5353from spatialdata_plot .pl .utils import (
54+ _RENDER_CMD_TO_CS_FLAG ,
5455 _get_cs_contents ,
5556 _get_elements_to_be_rendered ,
5657 _get_valid_cs ,
@@ -1011,9 +1012,11 @@ def show(
10111012 ax_x_min , ax_x_max = ax .get_xlim ()
10121013 ax_y_max , ax_y_min = ax .get_ylim () # (0, 0) is top-left
10131014
1014- coordinate_systems = sdata .coordinate_systems if coordinate_systems is None else coordinate_systems
1015+ cs_was_auto = coordinate_systems is None
1016+ coordinate_systems = list (sdata .coordinate_systems ) if cs_was_auto else coordinate_systems
10151017 if isinstance (coordinate_systems , str ):
10161018 coordinate_systems = [coordinate_systems ]
1019+ assert coordinate_systems is not None
10171020
10181021 for cs in coordinate_systems :
10191022 if cs not in sdata .coordinate_systems :
@@ -1037,14 +1040,32 @@ def show(
10371040 elements = elements_to_be_rendered ,
10381041 )
10391042
1040- # catch error in ruff-friendly way
1041- if ax is not None : # we'll generate matching number then
1043+ # When CS was auto-detected and ax is provided, keep only CS that have
1044+ # element types for ALL render commands (workaround for upstream #176).
1045+ if ax is not None :
10421046 n_ax = 1 if isinstance (ax , Axes ) else len (ax )
1047+ if cs_was_auto and len (coordinate_systems ) > n_ax :
1048+ required_flags = [_RENDER_CMD_TO_CS_FLAG [cmd ] for cmd in cmds if cmd in _RENDER_CMD_TO_CS_FLAG ]
1049+ strict_cs = [
1050+ cs_name
1051+ for cs_name in coordinate_systems
1052+ if all (cs_contents .query (f"cs == '{ cs_name } '" ).iloc [0 ][flag ] for flag in required_flags )
1053+ ]
1054+ if strict_cs :
1055+ coordinate_systems = strict_cs
1056+
10431057 if len (coordinate_systems ) != n_ax :
1044- raise ValueError (
1058+ msg = (
10451059 f"Mismatch between number of matplotlib axes objects ({ n_ax } ) "
10461060 f"and number of coordinate systems ({ len (coordinate_systems )} )."
10471061 )
1062+ if cs_was_auto :
1063+ msg += (
1064+ " This can happen when elements have transformations to multiple "
1065+ "coordinate systems (e.g. after filter_by_coordinate_system). "
1066+ "Pass `coordinate_systems=` explicitly to select which ones to plot."
1067+ )
1068+ raise ValueError (msg )
10481069
10491070 # set up canvas
10501071 fig_params , scalebar_params = _prepare_params_plot (
0 commit comments