Skip to content

Commit 1d44eab

Browse files
oscargusanntzer
andcommitted
Fix hatch linewidth in PGF
Co-authored-by: Antony Lee <anntzer.lee@gmail.com>
1 parent cf6069a commit 1d44eab

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

lib/matplotlib/backends/backend_pgf.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,7 @@ def __init__(self, figure, fh):
384384
self.figure = figure
385385
self.image_counter = 0
386386

387-
def draw_markers(self, gc, marker_path, marker_trans, path, trans,
388-
rgbFace=None):
387+
def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None):
389388
# docstring inherited
390389

391390
_writeln(self.fh, r"\begin{pgfscope}")
@@ -404,8 +403,7 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans,
404403
r"\pgfsys@defobject{currentmarker}"
405404
r"{\pgfqpoint{%fin}{%fin}}{\pgfqpoint{%fin}{%fin}}{" % coords)
406405
self._print_pgf_path(None, marker_path, marker_trans)
407-
self._pgf_path_draw(stroke=gc.get_linewidth() != 0.0,
408-
fill=rgbFace is not None)
406+
self._pgf_path_draw(stroke=gc.get_linewidth() != 0.0, fill=rgbFace is not None)
409407
_writeln(self.fh, r"}")
410408

411409
maxcoord = 16383 / 72.27 * self.dpi # Max dimensions in LaTeX.
@@ -429,8 +427,7 @@ def draw_path(self, gc, path, transform, rgbFace=None):
429427
self._print_pgf_clip(gc)
430428
self._print_pgf_path_styles(gc, rgbFace)
431429
self._print_pgf_path(gc, path, transform, rgbFace)
432-
self._pgf_path_draw(stroke=gc.get_linewidth() != 0.0,
433-
fill=rgbFace is not None)
430+
self._pgf_path_draw(stroke=gc.get_linewidth() != 0.0, fill=rgbFace is not None)
434431
_writeln(self.fh, r"\end{pgfscope}")
435432

436433
# if present, draw pattern on top
@@ -453,6 +450,8 @@ def draw_path(self, gc, path, transform, rgbFace=None):
453450
r"{\pgfqpoint{0in}{0in}}{\pgfqpoint{1in}{1in}}")
454451
_writeln(self.fh, r"\pgfusepath{clip}")
455452
scale = mpl.transforms.Affine2D().scale(self.dpi)
453+
lw = (mpl.rcParams["hatch.linewidth"] * mpl_pt_to_in * latex_in_to_pt)
454+
_writeln(self.fh, r"\pgfsetlinewidth{%fpt}" % lw)
456455
self._print_pgf_path(None, gc.get_hatch_path(), scale)
457456
self._pgf_path_draw(stroke=True)
458457
_writeln(self.fh, r"\end{pgfscope}")
@@ -464,8 +463,7 @@ def draw_path(self, gc, path, transform, rgbFace=None):
464463
xmin, xmax = f * xmin, f * xmax
465464
ymin, ymax = f * ymin, f * ymax
466465
repx, repy = math.ceil(xmax - xmin), math.ceil(ymax - ymin)
467-
_writeln(self.fh,
468-
r"\pgfsys@transformshift{%fin}{%fin}" % (xmin, ymin))
466+
_writeln(self.fh, r"\pgfsys@transformshift{%fin}{%fin}" % (xmin, ymin))
469467
for iy in range(repy):
470468
for ix in range(repx):
471469
_writeln(self.fh, r"\pgfsys@useobject{currentpattern}{}")
Binary file not shown.

lib/matplotlib/tests/test_backend_pgf.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99

1010
import matplotlib as mpl
1111
import matplotlib.pyplot as plt
12+
from matplotlib.backends.backend_pgf import FigureCanvasPgf, PdfPages
13+
from matplotlib.backends.backend_pdf import FigureCanvasPdf
1214
from matplotlib.testing import _has_tex_package, _check_for_pgf
13-
from matplotlib.testing.exceptions import ImageComparisonFailure
1415
from matplotlib.testing.compare import compare_images
15-
from matplotlib.backends.backend_pgf import PdfPages
16+
from matplotlib.testing.exceptions import ImageComparisonFailure
1617
from matplotlib.testing.decorators import (
1718
_image_directories, check_figures_equal, image_comparison)
1819
from matplotlib.testing._markers import (
@@ -400,3 +401,18 @@ def test_document_font_size():
400401
label=r'\normalsize the document font size is \the\fontdimen6\font'
401402
)
402403
plt.legend()
404+
405+
406+
@needs_pgf_xelatex
407+
@pytest.mark.backend('pgf')
408+
def test_pgf_hatch_linewidth():
409+
mpl.backend_bases.register_backend('pdf', FigureCanvasPgf)
410+
mpl.rcParams['hatch.linewidth'] = 0.1
411+
412+
plt.bar(1, 1, color='white', edgecolor='black', hatch='/')
413+
error = None
414+
try:
415+
compare_figure('hatch_linewidth.pdf')
416+
finally:
417+
# Restore backend to not mess up other tests
418+
mpl.backend_bases.register_backend('pdf', FigureCanvasPdf)

0 commit comments

Comments
 (0)