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

Skip to content

Commit 6e94330

Browse files
committed
Merge pull request #1858 from pwuertz/pgf_path_clip
backend_pgf: clip paths within the backend (fixes #1857)
2 parents 6e05499 + 70f2296 commit 6e94330

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

lib/matplotlib/backends/backend_pgf.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None)
421421
bl, tr = marker_path.get_extents(marker_trans).get_points()
422422
coords = bl[0] * f, bl[1] * f, tr[0] * f, tr[1] * f
423423
writeln(self.fh, r"\pgfsys@defobject{currentmarker}{\pgfqpoint{%fin}{%fin}}{\pgfqpoint{%fin}{%fin}}{" % coords)
424-
self._print_pgf_path(marker_path, marker_trans)
424+
self._print_pgf_path(None, marker_path, marker_trans)
425425
self._pgf_path_draw(stroke=gc.get_linewidth() != 0.0,
426426
fill=rgbFace is not None)
427427
writeln(self.fh, r"}")
@@ -441,7 +441,7 @@ def draw_path(self, gc, path, transform, rgbFace=None):
441441
# draw the path
442442
self._print_pgf_clip(gc)
443443
self._print_pgf_path_styles(gc, rgbFace)
444-
self._print_pgf_path(path, transform)
444+
self._print_pgf_path(gc, path, transform)
445445
self._pgf_path_draw(stroke=gc.get_linewidth() != 0.0,
446446
fill=rgbFace is not None)
447447
writeln(self.fh, r"\end{pgfscope}")
@@ -452,7 +452,7 @@ def draw_path(self, gc, path, transform, rgbFace=None):
452452

453453
# combine clip and path for clipping
454454
self._print_pgf_clip(gc)
455-
self._print_pgf_path(path, transform)
455+
self._print_pgf_path(gc, path, transform)
456456
writeln(self.fh, r"\pgfusepath{clip}")
457457

458458
# build pattern definition
@@ -461,7 +461,7 @@ def draw_path(self, gc, path, transform, rgbFace=None):
461461
writeln(self.fh, r"\pgfpathrectangle{\pgfqpoint{0in}{0in}}{\pgfqpoint{1in}{1in}}")
462462
writeln(self.fh, r"\pgfusepath{clip}")
463463
scale = mpl.transforms.Affine2D().scale(self.dpi)
464-
self._print_pgf_path(gc.get_hatch_path(), scale)
464+
self._print_pgf_path(None, gc.get_hatch_path(), scale)
465465
self._pgf_path_draw(stroke=True)
466466
writeln(self.fh, r"\end{pgfscope}")
467467
writeln(self.fh, r"}")
@@ -495,7 +495,7 @@ def _print_pgf_clip(self, gc):
495495
# check for clip path
496496
clippath, clippath_trans = gc.get_clip_path()
497497
if clippath is not None:
498-
self._print_pgf_path(clippath, clippath_trans)
498+
self._print_pgf_path(gc, clippath, clippath_trans)
499499
writeln(self.fh, r"\pgfusepath{clip}")
500500

501501
def _print_pgf_path_styles(self, gc, rgbFace):
@@ -543,10 +543,17 @@ def _print_pgf_path_styles(self, gc, rgbFace):
543543
dash_str += r"}{%fpt}" % dash_offset
544544
writeln(self.fh, dash_str)
545545

546-
def _print_pgf_path(self, path, transform):
546+
def _print_pgf_path(self, gc, path, transform):
547547
f = 1. / self.dpi
548+
# check for clip box
549+
bbox = gc.get_clip_rectangle() if gc else None
550+
if bbox:
551+
p1, p2 = bbox.get_points()
552+
clip = (p1[0], p1[1], p2[0], p2[1])
553+
else:
554+
clip = None
548555
# build path
549-
for points, code in path.iter_segments(transform):
556+
for points, code in path.iter_segments(transform, clip=clip):
550557
if code == Path.MOVETO:
551558
x, y = tuple(points)
552559
writeln(self.fh, r"\pgfpathmoveto{\pgfqpoint{%fin}{%fin}}" %

lib/matplotlib/compat/subprocess.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,5 @@ def _check_output(*popenargs, **kwargs):
7575
check_output = subprocess.check_output
7676
else:
7777
check_output = _check_output
78+
79+
CalledProcessError = subprocess.CalledProcessError

lib/matplotlib/tests/test_backend_pgf.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,21 @@ def test_rcupdate():
130130
create_figure()
131131
compare_figure('pgf_rcupdate%d.pdf' % (i+1))
132132

133+
134+
# test backend-side clipping, since large numbers are not supported by TeX
135+
@switch_backend('pgf')
136+
def test_pathclip():
137+
if not check_for('xelatex'):
138+
raise SkipTest('xelatex + pgf is required')
139+
140+
plt.figure()
141+
plt.plot([0., 1e100], [0., 1e100])
142+
plt.xlim(0, 1)
143+
plt.ylim(0, 1)
144+
# this test passes if compiling/saving to pdf works (no image comparison)
145+
plt.savefig(os.path.join(result_dir, "pgf_pathclip.pdf"))
146+
147+
133148
if __name__ == '__main__':
134149
import nose
135150
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

0 commit comments

Comments
 (0)