Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9e8acb2

Browse files
committed
Merge pull request #2423 from mdboom/cull-off-of-figure-markers
Off-axes markers unnecessarily saved to PDF
2 parents 3cefb7a + ea4f313 commit 9e8acb2

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1675,9 +1675,17 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans,
16751675

16761676
output(Op.gsave)
16771677
lastx, lasty = 0, 0
1678-
for vertices, code in path.iter_segments(trans, simplify=False):
1678+
for vertices, code in path.iter_segments(
1679+
trans,
1680+
clip=(0, 0, self.file.width*72, self.file.height*72),
1681+
simplify=False):
16791682
if len(vertices):
16801683
x, y = vertices[-2:]
1684+
if (x < 0 or
1685+
y < 0 or
1686+
x > self.file.width * 72 or
1687+
y > self.file.height * 72):
1688+
continue
16811689
dx, dy = x - lastx, y - lasty
16821690
output(1, 0, 0, 1, dx, dy, Op.concat_matrix,
16831691
marker, Op.use_xobject)

lib/matplotlib/backends/backend_ps.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,10 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None)
617617
ps_cmd.append('stroke')
618618
ps_cmd.extend(['grestore', '} bind def'])
619619

620-
for vertices, code in path.iter_segments(trans, simplify=False):
620+
for vertices, code in path.iter_segments(
621+
trans,
622+
clip=(0, 0, self.width*72, self.height*72),
623+
simplify=False):
621624
if len(vertices):
622625
x, y = vertices[-2:]
623626
ps_cmd.append("%g %g o" % (x, y))

lib/matplotlib/backends/backend_svg.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,9 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None)
593593

594594
trans_and_flip = self._make_flip_transform(trans)
595595
attrib = {'xlink:href': '#%s' % oid}
596-
for vertices, code in path.iter_segments(trans_and_flip, simplify=False):
596+
clip = (0, 0, self.width*72, self.height*72)
597+
for vertices, code in path.iter_segments(
598+
trans_and_flip, clip=clip, simplify=False):
597599
if len(vertices):
598600
x, y = vertices[-2:]
599601
attrib['x'] = six.text_type(x)

lib/matplotlib/tests/test_backend_pdf.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ def test_type42():
4343
@cleanup
4444
def test_multipage_pagecount():
4545
from matplotlib.backends.backend_pdf import PdfPages
46-
from io import BytesIO
47-
with PdfPages(BytesIO()) as pdf:
46+
with PdfPages(io.BytesIO()) as pdf:
4847
assert pdf.get_pagecount() == 0
4948
fig = plt.figure()
5049
ax = fig.add_subplot(111)
@@ -55,6 +54,21 @@ def test_multipage_pagecount():
5554
assert pdf.get_pagecount() == 2
5655

5756

57+
@cleanup
58+
def test_cull_markers():
59+
x = np.random.random(20000)
60+
y = np.random.random(20000)
61+
62+
fig = plt.figure()
63+
ax = fig.add_subplot(111)
64+
ax.plot(x, y, 'k.')
65+
ax.set_xlim(2, 3)
66+
67+
pdf = io.BytesIO()
68+
fig.savefig(pdf, format="pdf")
69+
assert len(pdf.getvalue()) < 8000
70+
71+
5872
@cleanup
5973
def test_multipage_keep_empty():
6074
from matplotlib.backends.backend_pdf import PdfPages

0 commit comments

Comments
 (0)