diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index a966cef7a981..f0773e0c4607 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -2283,6 +2283,7 @@ def delta(self, other): needed to transform self into other. """ cmds = [] + fill_performed = False for params, cmd in self.commands: different = False for p in params: @@ -2301,7 +2302,13 @@ def delta(self, other): if different: break + # Need to update hatching if we also updated fillcolor + if params == ('_hatch',) and fill_performed: + different = True + if different: + if params == ('_fillcolor',): + fill_performed = True theirs = [getattr(other, p) for p in params] cmds.extend(cmd(self, *theirs)) for p in params: diff --git a/lib/matplotlib/tests/baseline_images/test_backend_pdf/hatching_legend.pdf b/lib/matplotlib/tests/baseline_images/test_backend_pdf/hatching_legend.pdf new file mode 100644 index 000000000000..146d4dd92d4d Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_backend_pdf/hatching_legend.pdf differ diff --git a/lib/matplotlib/tests/test_backend_pdf.py b/lib/matplotlib/tests/test_backend_pdf.py index 6ba1e4a5c36e..5527fd21c01b 100644 --- a/lib/matplotlib/tests/test_backend_pdf.py +++ b/lib/matplotlib/tests/test_backend_pdf.py @@ -107,3 +107,15 @@ def test_composite_image(): with PdfPages(io.BytesIO()) as pdf: fig.savefig(pdf, format="pdf") assert len(pdf._file.images.keys()) == 2 + + +@image_comparison(baseline_images=['hatching_legend'], + extensions=['pdf']) +def test_hatching_legend(): + """Test for correct hatching on patches in legend""" + fig = plt.figure(figsize=(1, 2)) + + a = plt.Rectangle([0, 0], 0, 0, facecolor="green", hatch="XXXX") + b = plt.Rectangle([0, 0], 0, 0, facecolor="blue", hatch="XXXX") + + fig.legend([a, b, a, b], ["", "", "", ""])