Skip to content

Commit f95d761

Browse files
authored
Skip off-canvas markers in PDF backend, accounting for marker size
1 parent 4104dd5 commit f95d761

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

lib/matplotlib/backends/backend_pdf.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,12 +2104,26 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
21042104

21052105
padding = np.max(linewidths)
21062106
path_codes = []
2107+
2108+
# Calculate maximum marker extent for conservative bounds checking.
2109+
# We need to account for marker size, not just position.
2110+
max_marker_extent = 0
21072111
for i, (path, transform) in enumerate(self._iter_collection_raw_paths(
21082112
master_transform, paths, all_transforms)):
2113+
if len(path.vertices):
2114+
# Get the bounding box of the transformed marker path.
2115+
# Use get_extents() which is more efficient than transforming
2116+
# all vertices, and add padding for stroke width.
2117+
bbox = path.get_extents(transform)
2118+
max_marker_extent = max(max_marker_extent,
2119+
bbox.width / 2, bbox.height / 2)
21092120
name = self.file.pathCollectionObject(
21102121
gc, path, transform, padding, filled, stroked)
21112122
path_codes.append(name)
21122123

2124+
# Add padding for stroke width.
2125+
max_marker_extent += padding
2126+
21132127
output = self.file.output
21142128
output(*self.gc.push())
21152129
lastx, lasty = 0, 0
@@ -2118,10 +2132,13 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
21182132
facecolors, edgecolors, linewidths, linestyles,
21192133
antialiaseds, urls, offset_position, hatchcolors=hatchcolors):
21202134

2121-
# Skip markers outside visible canvas bounds to reduce PDF size
2122-
# (same optimization as in draw_markers).
2123-
if not (0 <= xo <= self.file.width * 72
2124-
and 0 <= yo <= self.file.height * 72):
2135+
# Skip markers outside visible canvas bounds to reduce PDF size.
2136+
# Add max_marker_extent margin to account for marker size - a marker
2137+
# may be partially visible even if its center is outside the canvas.
2138+
canvas_width = self.file.width * 72
2139+
canvas_height = self.file.height * 72
2140+
if not (-max_marker_extent <= xo <= canvas_width + max_marker_extent
2141+
and -max_marker_extent <= yo <= canvas_height + max_marker_extent):
21252142
continue
21262143

21272144
self.check_gc(gc0, rgbFace)

0 commit comments

Comments
 (0)