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

Skip to content

Fix hatch linewidth in PGF #24263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions lib/matplotlib/backends/backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,7 @@ def __init__(self, figure, fh):
self.figure = figure
self.image_counter = 0

def draw_markers(self, gc, marker_path, marker_trans, path, trans,
rgbFace=None):
def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None):
# docstring inherited

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

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

# if present, draw pattern on top
Expand All @@ -453,6 +450,8 @@ def draw_path(self, gc, path, transform, rgbFace=None):
r"{\pgfqpoint{0in}{0in}}{\pgfqpoint{1in}{1in}}")
_writeln(self.fh, r"\pgfusepath{clip}")
scale = mpl.transforms.Affine2D().scale(self.dpi)
lw = (mpl.rcParams["hatch.linewidth"] * mpl_pt_to_in * latex_in_to_pt)
_writeln(self.fh, r"\pgfsetlinewidth{%fpt}" % lw)
self._print_pgf_path(None, gc.get_hatch_path(), scale)
self._pgf_path_draw(stroke=True)
_writeln(self.fh, r"\end{pgfscope}")
Expand All @@ -464,8 +463,7 @@ def draw_path(self, gc, path, transform, rgbFace=None):
xmin, xmax = f * xmin, f * xmax
ymin, ymax = f * ymin, f * ymax
repx, repy = math.ceil(xmax - xmin), math.ceil(ymax - ymin)
_writeln(self.fh,
r"\pgfsys@transformshift{%fin}{%fin}" % (xmin, ymin))
_writeln(self.fh, r"\pgfsys@transformshift{%fin}{%fin}" % (xmin, ymin))
for iy in range(repy):
for ix in range(repx):
_writeln(self.fh, r"\pgfsys@useobject{currentpattern}{}")
Expand Down
Binary file not shown.
20 changes: 18 additions & 2 deletions lib/matplotlib/tests/test_backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pgf import FigureCanvasPgf, PdfPages
from matplotlib.backends.backend_pdf import FigureCanvasPdf
from matplotlib.testing import _has_tex_package, _check_for_pgf
from matplotlib.testing.exceptions import ImageComparisonFailure
from matplotlib.testing.compare import compare_images
from matplotlib.backends.backend_pgf import PdfPages
from matplotlib.testing.exceptions import ImageComparisonFailure
from matplotlib.testing.decorators import (
_image_directories, check_figures_equal, image_comparison)
from matplotlib.testing._markers import (
Expand Down Expand Up @@ -400,3 +401,18 @@ def test_document_font_size():
label=r'\normalsize the document font size is \the\fontdimen6\font'
)
plt.legend()


@needs_pgf_xelatex
@pytest.mark.backend('pgf')
def test_pgf_hatch_linewidth():
mpl.backend_bases.register_backend('pdf', FigureCanvasPgf)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering how many things are failing, this must be a global setting that isn't reset.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, good observation! That is indeed a good first thing to fix. I'll give it a try.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this works as expected (cannot run matplotlib from source locally for unknown reasons).

Main problem was how to change the backend back after the test. One may consider adding an argument to the image_comparison decorator that takes a lambda and evaluates it after the comparison is done.

I also considered using check_figure_equal and mocking two classes that has a save_fig method that sets backend, but didn't understand how to deal with the close call later on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at it again, the tests in this file use pytest.mark.backend('pgf') with image_comparison(['foo.pdf']); do we really need the extra registration or are the other tests not working as expected?

mpl.rcParams['hatch.linewidth'] = 0.1

plt.bar(1, 1, color='white', edgecolor='black', hatch='/')
error = None
try:
compare_figure('hatch_linewidth.pdf')
finally:
# Restore backend to not mess up other tests
mpl.backend_bases.register_backend('pdf', FigureCanvasPdf)
Loading