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

Skip to content

Commit 38ab792

Browse files
committed
Support sketch_params in pgf backend
Fixes #20516 PGF's `random steps` decoration seems to be the most similar, but does not exactly match the behaviour described in matplotlib's docs. Therefore I repurposed the `randomness` argument as a seed to give control on how the line looks afterwards.
1 parent 1d12973 commit 38ab792

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/matplotlib/artist.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,17 @@ def set_sketch_params(self, scale=None, length=None, randomness=None):
677677
"""
678678
Set the sketch parameters.
679679
680+
Requires the following preamble when using the PGF backend:
681+
682+
\\usepackage{pgf}
683+
\\usepgfmodule{decorations}
684+
\\usepgflibrary{decorations.pathmorphing}
685+
686+
This also applies to PGF backend + PDF output, where this must be added
687+
to `pgf.preamble` manually. The PGF backend uses the `randomness`
688+
argument as a seed and not as described below. Pass the same seed to
689+
obtain the same random shape.
690+
680691
Parameters
681692
----------
682693
scale : float, optional

lib/matplotlib/backends/backend_pgf.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,21 @@ def _print_pgf_path(self, gc, path, transform, rgbFace=None):
600600
r"{\pgfqpoint{%fin}{%fin}}"
601601
% coords)
602602

603+
# apply pgf decorators
604+
sketch_params = gc.get_sketch_params() if gc else None
605+
if sketch_params is not None:
606+
# Only "length" directly maps to "segment length" in PGF's API
607+
# The others are combined in "amplitude" -> Use "randomness" as
608+
# PRNG seed to allow the user to force the same shape on multiple
609+
# sketched lines
610+
scale, length, randomness = sketch_params
611+
if scale is not None:
612+
writeln(self.fh, r"\pgfkeys{/pgf/decoration/.cd, "
613+
f"segment length = {(length * f):f}in, "
614+
f"amplitude = {(scale * f):f}in}}")
615+
writeln(self.fh, f"\\pgfmathsetseed{{{int(randomness)}}}")
616+
writeln(self.fh, r"\pgfdecoratecurrentpath{random steps}")
617+
603618
def _pgf_path_draw(self, stroke=True, fill=False):
604619
actions = []
605620
if stroke:

0 commit comments

Comments
 (0)