From ea4f313bf2e821b1b4e6eee993461e9f3ecca3e2 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 1 Oct 2013 18:13:15 -0400 Subject: [PATCH] Fix #2423: Clip markers off figure. --- lib/matplotlib/backends/backend_pdf.py | 10 +++++++++- lib/matplotlib/backends/backend_ps.py | 5 ++++- lib/matplotlib/backends/backend_svg.py | 4 +++- lib/matplotlib/tests/test_backend_pdf.py | 18 ++++++++++++++++-- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 25df714c6088..cb1c552d1492 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -1615,9 +1615,17 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None) output(Op.gsave) lastx, lasty = 0, 0 - for vertices, code in path.iter_segments(trans, simplify=False): + for vertices, code in path.iter_segments( + trans, + clip=(0, 0, self.file.width*72, self.file.height*72), + simplify=False): if len(vertices): x, y = vertices[-2:] + if (x < 0 or + y < 0 or + x > self.file.width * 72 or + y > self.file.height * 72): + continue dx, dy = x - lastx, y - lasty output(1, 0, 0, 1, dx, dy, Op.concat_matrix, marker, Op.use_xobject) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index 9fd88f9d5e70..2506167e70bd 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -615,7 +615,10 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None) ps_cmd.append('stroke') ps_cmd.extend(['grestore', '} bind def']) - for vertices, code in path.iter_segments(trans, simplify=False): + for vertices, code in path.iter_segments( + trans, + clip=(0, 0, self.width*72, self.height*72), + simplify=False): if len(vertices): x, y = vertices[-2:] ps_cmd.append("%g %g o" % (x, y)) diff --git a/lib/matplotlib/backends/backend_svg.py b/lib/matplotlib/backends/backend_svg.py index f68801cfd229..f096bbb734f1 100644 --- a/lib/matplotlib/backends/backend_svg.py +++ b/lib/matplotlib/backends/backend_svg.py @@ -593,7 +593,9 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None) trans_and_flip = self._make_flip_transform(trans) attrib = {'xlink:href': '#%s' % oid} - for vertices, code in path.iter_segments(trans_and_flip, simplify=False): + clip = (0, 0, self.width*72, self.height*72) + for vertices, code in path.iter_segments( + trans_and_flip, clip=clip, simplify=False): if len(vertices): x, y = vertices[-2:] attrib['x'] = six.text_type(x) diff --git a/lib/matplotlib/tests/test_backend_pdf.py b/lib/matplotlib/tests/test_backend_pdf.py index 827d6743e55f..636758bf71dd 100644 --- a/lib/matplotlib/tests/test_backend_pdf.py +++ b/lib/matplotlib/tests/test_backend_pdf.py @@ -41,8 +41,7 @@ def test_type42(): @cleanup def test_multipage_pagecount(): from matplotlib.backends.backend_pdf import PdfPages - from io import BytesIO - with PdfPages(BytesIO()) as pdf: + with PdfPages(io.BytesIO()) as pdf: assert pdf.get_pagecount() == 0 fig = plt.figure() ax = fig.add_subplot(111) @@ -53,6 +52,21 @@ def test_multipage_pagecount(): assert pdf.get_pagecount() == 2 +@cleanup +def test_cull_markers(): + x = np.random.random(20000) + y = np.random.random(20000) + + fig = plt.figure() + ax = fig.add_subplot(111) + ax.plot(x, y, 'k.') + ax.set_xlim(2, 3) + + pdf = io.BytesIO() + fig.savefig(pdf, format="pdf") + assert len(pdf.getvalue()) < 8000 + + @cleanup def test_multipage_keep_empty(): from matplotlib.backends.backend_pdf import PdfPages