@@ -239,6 +239,31 @@ def extract_title_from_figure(fig: Figure | DeephavenFigure) -> str | None:
239239 return str (title )
240240
241241
242+ def map_user_index_to_grid_position (idx : int , rows : int , cols : int ) -> tuple [int , int ]:
243+ """Map user's natural index to reversed grid position
244+
245+ Since the grid is reversed internally (to match plotly's bottom-to-top
246+ coordinate system), we need to convert user's natural row-major index
247+ (top-to-bottom, left-to-right) to the reversed grid position.
248+
249+ Args:
250+ idx: User's index in natural row-major order (0 = top-left)
251+ rows: Number of rows
252+ cols: Number of columns
253+
254+ Returns:
255+ Tuple of (row, col) in reversed grid coordinates
256+
257+ """
258+ # Calculate row and col from index (row-major order in user's view)
259+ user_row = idx // cols
260+ col = idx % cols
261+ # Reverse the row index to match the reversed grid
262+ # User's row 0 (top) maps to reversed grid's row (rows-1)
263+ row = (rows - 1 ) - user_row
264+ return row , col
265+
266+
242267def create_subplot_annotations (
243268 titles : list [str ],
244269 col_starts : list [float ],
@@ -266,21 +291,17 @@ def create_subplot_annotations(
266291 annotations = []
267292
268293 for idx , title in enumerate (titles ):
269- if not title : # Skip empty titles
294+ # Skip empty or whitespace-only titles
295+ if not title or (isinstance (title , str ) and not title .strip ()):
270296 continue
271297
272- # Calculate row and col from index (row-major order in user's view)
273- # Since the grid is reversed internally, we need to map user's row order
274- # to the reversed grid. User's row 0 (top) maps to reversed grid's row (rows-1)
275- user_row = idx // cols
276- col = idx % cols
277- # Reverse the row index to match the reversed grid
278- row = (rows - 1 ) - user_row
298+ # Map user's natural index to reversed grid position
299+ row , col = map_user_index_to_grid_position (idx , rows , cols )
279300
280301 # Calculate x position (center of column)
281302 x = (col_starts [col ] + col_ends [col ]) / 2
282303
283- # Calculate y position (top of row with small offset )
304+ # Calculate y position (top of row)
284305 y = row_ends [row ]
285306
286307 annotation = {
0 commit comments