Skip to content

Commit 447e578

Browse files
committed
PGF: Clip lines/markers to maximum LaTeX dimensions.
This has been open for many years without any change to LaTeX, so it seems unlikely they are going to change how their parsing works. Fixes matplotlib#4482.
1 parent 4a0c57b commit 447e578

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

lib/matplotlib/backends/backend_pgf.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,12 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans,
426426
fill=rgbFace is not None)
427427
writeln(self.fh, r"}")
428428

429+
maxcoord = 16383 / 72.27 * self.dpi # Max dimensions in LaTeX.
430+
clip = (-maxcoord, -maxcoord, maxcoord, maxcoord)
431+
429432
# draw marker for each vertex
430-
for point, code in path.iter_segments(trans, simplify=False):
433+
for point, code in path.iter_segments(trans, simplify=False,
434+
clip=clip):
431435
x, y = point[0] * f, point[1] * f
432436
writeln(self.fh, r"\begin{pgfscope}")
433437
writeln(self.fh, r"\pgfsys@transformshift{%fin}{%fin}" % (x, y))
@@ -564,11 +568,13 @@ def _print_pgf_path(self, gc, path, transform, rgbFace=None):
564568
f = 1. / self.dpi
565569
# check for clip box / ignore clip for filled paths
566570
bbox = gc.get_clip_rectangle() if gc else None
571+
maxcoord = 16383 / 72.27 * self.dpi # Max dimensions in LaTeX.
567572
if bbox and (rgbFace is None):
568573
p1, p2 = bbox.get_points()
569-
clip = (p1[0], p1[1], p2[0], p2[1])
574+
clip = (max(p1[0], -maxcoord), max(p1[1], -maxcoord),
575+
min(p2[0], maxcoord), min(p2[1], maxcoord))
570576
else:
571-
clip = None
577+
clip = (-maxcoord, -maxcoord, maxcoord, maxcoord)
572578
# build path
573579
for points, code in path.iter_segments(transform, clip=clip):
574580
if code == Path.MOVETO:

lib/matplotlib/tests/test_backend_pgf.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,19 @@ def test_rcupdate():
150150
@pytest.mark.style('default')
151151
@pytest.mark.backend('pgf')
152152
def test_pathclip():
153+
np.random.seed(19680801)
153154
mpl.rcParams.update({'font.family': 'serif', 'pgf.rcfonts': False})
154-
plt.plot([0., 1e100], [0., 1e100])
155-
plt.xlim(0, 1)
156-
plt.ylim(0, 1)
157-
plt.savefig(BytesIO(), format="pdf") # No image comparison.
155+
fig, axs = plt.subplots(1, 2)
156+
157+
axs[0].plot([0., 1e100], [0., 1e100])
158+
axs[0].set_xlim(0, 1)
159+
axs[0].set_ylim(0, 1)
160+
161+
axs[1].scatter([0, 1], [1, 1])
162+
axs[1].hist(np.random.normal(size=1000), bins=20, range=[-10, 10])
163+
axs[1].set_xscale('log')
164+
165+
fig.savefig(BytesIO(), format="pdf") # No image comparison.
158166

159167

160168
# test mixed mode rendering

0 commit comments

Comments
 (0)