Skip to content

Commit 7da8dfc

Browse files
committed
fix: make overlap metric more robust
1 parent 818363d commit 7da8dfc

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/mplhep/_utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,20 @@ def _overlap(ax, bboxes, get_vertices=False, exclude_texts=None):
11071107
vertices_display = ax.transData.transform(path.vertices)
11081108
lines_display.append(vertices_display)
11091109

1110+
# For step/line paths, also sample intermediate points along edges
1111+
# This ensures we detect overlap with horizontal/vertical segments
1112+
if len(path.vertices) > 1:
1113+
for i in range(len(path.vertices) - 1):
1114+
v1 = path.vertices[i]
1115+
v2 = path.vertices[i + 1]
1116+
# Sample 5 intermediate points along each edge
1117+
t = np.linspace(0, 1, 7)[
1118+
1:-1
1119+
] # Exclude endpoints (already in vertices)
1120+
interpolated = v1 + (v2 - v1) * t[:, np.newaxis]
1121+
interpolated_display = ax.transData.transform(interpolated)
1122+
lines_display.append(interpolated_display)
1123+
11101124
# Collect bboxes from texts (excluding specified text objects to avoid self-overlap)
11111125
for handle in ax.texts:
11121126
if isinstance(handle, Text) and handle not in exclude_texts:
@@ -1146,6 +1160,19 @@ def _overlap(ax, bboxes, get_vertices=False, exclude_texts=None):
11461160
all_vertices_display
11471161
) + bbox.count_overlaps(bboxes_display)
11481162

1163+
# Also check if data vertices are above the text bbox (occluding it)
1164+
# This catches cases where the histogram bars cover the text
1165+
for bbox in bboxes:
1166+
if len(all_vertices_display) > 0:
1167+
# Check if any vertices are in the same x-range but above the bbox
1168+
in_x_range = (all_vertices_display[:, 0] >= bbox.x0) & (
1169+
all_vertices_display[:, 0] <= bbox.x1
1170+
)
1171+
above_bbox = all_vertices_display[:, 1] > bbox.y1
1172+
occluding = in_x_range & above_bbox
1173+
if occluding.any():
1174+
overlap_count += occluding.sum()
1175+
11491176
logger.info(f"Overlap count: {overlap_count}")
11501177

11511178
if _DEBUG_OVERLAP:

0 commit comments

Comments
 (0)