Skip to content

Commit 7a5f519

Browse files
Copilotjnumainville
andcommitted
Refactor annotation logic: extract helper function and improve empty title check
Co-authored-by: jnumainville <[email protected]>
1 parent 287266f commit 7a5f519

File tree

1 file changed

+30
-9
lines changed
  • plugins/plotly-express/src/deephaven/plot/express/plots

1 file changed

+30
-9
lines changed

plugins/plotly-express/src/deephaven/plot/express/plots/subplots.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
242267
def 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

Comments
 (0)