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

Skip to content

Commit ea4f313

Browse files
committed
Fix #2423: Clip markers off figure.
1 parent 926bddc commit ea4f313

4 files changed

Lines changed: 32 additions & 5 deletions

File tree

lib/matplotlib/backends/backend_pdf.py

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

16161616
output(Op.gsave)
16171617
lastx, lasty = 0, 0
1618-
for vertices, code in path.iter_segments(trans, simplify=False):
1618+
for vertices, code in path.iter_segments(
1619+
trans,
1620+
clip=(0, 0, self.file.width*72, self.file.height*72),
1621+
simplify=False):
16191622
if len(vertices):
16201623
x, y = vertices[-2:]
1624+
if (x < 0 or
1625+
y < 0 or
1626+
x > self.file.width * 72 or
1627+
y > self.file.height * 72):
1628+
continue
16211629
dx, dy = x - lastx, y - lasty
16221630
output(1, 0, 0, 1, dx, dy, Op.concat_matrix,
16231631
marker, Op.use_xobject)

lib/matplotlib/backends/backend_ps.py

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

618-
for vertices, code in path.iter_segments(trans, simplify=False):
618+
for vertices, code in path.iter_segments(
619+
trans,
620+
clip=(0, 0, self.width*72, self.height*72),
621+
simplify=False):
619622
if len(vertices):
620623
x, y = vertices[-2:]
621624
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
@@ -41,8 +41,7 @@ def test_type42():
4141
@cleanup
4242
def test_multipage_pagecount():
4343
from matplotlib.backends.backend_pdf import PdfPages
44-
from io import BytesIO
45-
with PdfPages(BytesIO()) as pdf:
44+
with PdfPages(io.BytesIO()) as pdf:
4645
assert pdf.get_pagecount() == 0
4746
fig = plt.figure()
4847
ax = fig.add_subplot(111)
@@ -53,6 +52,21 @@ def test_multipage_pagecount():
5352
assert pdf.get_pagecount() == 2
5453

5554

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

0 commit comments

Comments
 (0)